I am trying to build a clock. You can see the working code here. However, I feel like I can do much better in the JavaScript code.
var getDate = function getDate() {
var date = new Date();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var ampm = 'pm'; // by default, it is pm
var showDate;
var showTime;
if (hour < 10) {
hour = '0' + hour;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
if (hour < 12) {
ampm = 'am';
}
if (hour > 12) {
hour -= 12;
}
showDate = monthNames[month] + ' ' + day;
showTime = hour + ':' + minutes + ampm;
document.getElementById('js-date').innerHTML = showDate;
document.getElementById('js-time').innerHTML = showTime;
requestAnimationFrame(getDate);
};
getDate();