简体   繁体   中英

The formula for calculating a day on a given date works fine but not on some specific dates

Some days ago, I have found a useful link in which a day on a given date is displayed. I found it very useful for embedding it into some specific applications. It works fine but on some dates, it does not work. Some of dates on which this formula does not work are as follows.

(dd/mm/yyyy)

1/3/1901

2/3/1901

3/3/1901

1/3/1902

2/3/1902

and so on...

The link where this formula was implemented in Javascript is as follows. Just follow it and try the above dates in three textfields given. It will display "undefined" means that it may not be specific to obtain the day on such dates. What should actually be the reason, any idea?

The relevant javascript code is:

var monthCodes = Array(5,1,1,4,6,2,4,0,3,5,1,3);
var weekdays = Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
function compute() {
    d = conversion.day.value;
    m = conversion.month.value;
    y = conversion.year.value%100;
    c = Math.floor(conversion.year.value/100);
    if((conversion.year.value%4==0) && (conversion.year.value%100!=0 || conversion.year.value%400==0) && (m==1 || m==2))
        isLeapJanFeb = 1;
    else
        isLeapJanFeb = 0;
    weekday = (1*d + 1*monthCodes[m-1] + 1*y + 1*Math.floor(y/4) - 2*(c%4) - 1*isLeapJanFeb) % 7;
    alert(weekdays[weekday]);
}

Note that it works fine in Internet explorer. In some browsers like Mozilla Firefox, it may not work.

Here is the link...

http://katzentier.de/_misc/perpetual_calendar.htm

Well yes you found a bug in the JavaScript.

I did a view source and copied it to a fiddle, but JSFiddle appears to be down now. Here is the code:

var monthCodes = Array(5,1,1,4,6,2,4,0,3,5,1,3);
var weekdays =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
var d = +prompt("Day");
var m = +prompt("Month");
var year = +prompt("Year");
var y = year%100;
var c = Math.floor(year/100);
if ((year%4==0) && (year%100!=0 || year%400==0) && (m==1 || m==2))
    isLeapJanFeb = 1;
else
    isLeapJanFeb = 0;
weekday = Math.floor(1*d + 1*monthCodes[m-1] + 1*y + 1*Math.floor(y/4) - 2*(c%4) - 1*isLeapJanFeb) % 7;
alert("Weekday code computed to be " + weekday);
alert("The weekday is " + weekdays[weekday]);

If you use today's date it will work, but try 1 2 1901.

The problem is that the computation gives -3, and -3 % 7 is -3 and when you index an array with -3 you get undefined.

Maybe some browsers interpret a negative array index as something other than undefined. Perhaps their JS engine works like Ruby and for weekdays[-3] they count from the right end, because after all February 1, 1901 was indeed a Friday!

Again, someone posted some bad code on the internet! Obligatory xkcd reference!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM