Pure Javascript date month year

 

<!DOCTYPE html>
<html>
<body>
<script>
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];

var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();

return day + ' ' + monthNames[monthIndex] + ' ' + year;
}

// Alternative od document ready in pure js

document.addEventListener('DOMContentLoaded', function(){
document.getElementById('today').innerHTML='Today is :' +formatDate(new Date());
});

 

</script>
<div id="today"></div>
</body>
</html>

Leave comment

Your email address will not be published. Required fields are marked with *.