Incorrect day in clock function

epiz_24820641

Not getting an error message however, when the results are displayed, The day is showing up wrong. All other variables are listed correctly. I have tested this on multiple machines.

I am using the java function below:

window.onload = function() {
clock();
function clock() {
var now = new Date();
var TwentyFourHour = now.getHours();
var hour = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
var mid = ‘pm’;
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDay();
if (month = 1) {
month = “January”;
}
if (month = 2) {
month = “February”;
}
if (month = 3) {
month = “March”;
}
if (month = 4) {
month = “April”;
}
if (month = 5) {
month = “May”;
}
if (month = 6) {
month = “June”;
}
if (month = 7) {
month = “July”;
}
if (month = 8) {
month = “August”;
}
if (month = 9) {
month = “September”;
}
if (month = 10) {
month = “October”;
}
if (month = 11) {
month = “November”;
}
if (month = 12) {
month = “December”;
}
if (sec < 10) {
sec = “0” + sec;
}
if (min < 10) {
min = “0” + min;
}
if (hour > 12) {
hour = hour - 12;
}
if(hour==0){
hour=12;
}
if(TwentyFourHour < 12) {
mid = ‘am’;
}
document.getElementById(‘currentTime’).innerHTML = “”+month+" “+day+”, “+year+”
"+hour+’:’+min+’:’+sec +’ '+mid+"" ;
setTimeout(clock, 1000);
}
}

Maybe you should explain how it is incorrect?

1 Like

Just to be clear: Javascript runs in the browser of your visitors, not on our servers. So any error encountered in it can only be caused by your computer, your browser or your code.

Googling a few things for a minute: are you aware that the getDay function returns the day of the week? If you want to get the day of the month, the function getDate is the one you’re looking for.

1 Like

Today is December 28, 2020
The results are showing as:

December 1, 2020

That corrected my issue. Apologies for my lack of research and ignorance. Thank you very much.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.