JS

How to Show JavaScript datetime in 12 hour AM/PM format and show Month Name with date?

In this article we will see How to Show JavaScript datetime in 12 hour AM/PM format and show Month Name with date?
We will show here JavaScript datetime in 12 hour AM/PM format and show Month Name with date like February 24,2019 3:41 pm

Simply copy this code and paste in any editor like notepad,Notepadd++ or as you wish for JavaScript datetime in 12 hour AM/PM format and show Month Name with date like February 24,2019 3:41 pm

[php]
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script>
//Here I have taken one const variable and store all month name
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
//Create a function for show hours according to AM PM
function showAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? ‘pm’ : ‘am’;
hours = hours % 12;
hours = hours ? hours : 12; // the hour ‘0’ should be ’12’
minutes = minutes < 10 ? ‘0’ + minutes : minutes;
var strTime = hours + ‘:’ + minutes + ‘ ‘ + ampm;
return strTime;
}

const d = new Date();
var day = d.getDate();

var output = monthNames[d.getMonth()] + ‘ ‘ + day + ‘,’ + d.getFullYear() + ‘ ‘ + showAMPM(new Date())
alert(output);

</script>
</head>
<body>
</body>
</html>
[/php]

Screenshot Show output after run this page.

Leave a Reply

Your email address will not be published.