Datejs:获取一周的日期编号

时间:2012-10-03 18:54:07

标签: javascript

Using Datejs - Get the day of the week

我也在使用http://www.datejs.com。我也一定想错过它。有没有办法让我有一周的天数

我知道我可以使用数组等价,但是库很好,我觉得我很想念它,我到处寻找。

更新

我也知道我可以使用日期getDay方法,但我认为有一个datejs替代方法可以纠正内置日期对象的一些奇怪的行为。

3 个答案:

答案 0 :(得分:1)

您可以使用标准Date方法获取号码,getDaygetUTCday

new Date('2012-10-03').getDay(); // 2

答案 1 :(得分:1)

只需使用Date对象上的内置getDay函数:

new Date().getDay();

答案 2 :(得分:0)

此代码通过计算其中一种算法来计算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");