此代碼計算一天1700/1/1之後的日期通過其中一種算法來計算它
var weekDay = function(year, month, day) {
var offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var week = {0:'Sunday',
1:'Monday',
2:'Tuesday',
3:'Wednesday',
4:'Thursday',
5:'Friday',
6:'Saturday'};
var afterFeb = (month > 2)? 0 : 1;
aux = year - 1700 - afterFeb;
// dayOfWeek for 1700/1/1 = 5, Friday
dayOfWeek = 5;
// partial sum of days betweem current date and 1700/1/1
dayOfWeek += (aux + afterFeb) * 365;
// leap year correction
dayOfWeek += parseInt(aux/4) -
parseInt(aux/100) +
parseInt((aux + 100)/400);
// sum monthly and day offsets
dayOfWeek += offset[month - 1] + (day - 1);
dayOfWeek = parseInt(dayOfWeek % 7);
return [dayOfWeek, week[dayOfWeek]];
};
console.log(weekDay(2013, 6, 15)[0] == 6, weekDay(2013, 6, 15)[1] == "Saturday");
console.log(weekDay(1969, 7, 20)[0] == 0, weekDay(1969, 7, 20)[1] == "Sunday");
console.log(weekDay(1945, 4, 30)[0] == 1, weekDay(1945, 4, 30)[1] == "Monday");
console.log(weekDay(1900, 1, 1)[0] == 1, weekDay(1900, 1, 1)[1] == "Monday");
console.log(weekDay(1789, 7, 14)[0] == 2, weekDay(1789, 7, 14)[1] == "Tuesday");
我想你只是使用getDay,datejs擴展了Date原型,但它的原始功能應該仍然可以正常工作。 – PherricOxide
但datejs的存在,以糾正原來的日期對象錯誤,所以我認爲有一個替代getDay方法。 – lolol
@lolol Date.js主要用於* extend *原始的Date對象,添加了通常不需要的方法,比如自定義格式。 「日期」對象中的「*修復*」並不多。 Date.js本身[使用'getDay()'](http://code.google.com/p/datejs/source/browse/trunk/src/core.js#837)(837-840行)。 –