0

使用 Datejs - 获取星期几

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

我知道我可以使用数组等价,但是图书馆非常好,我想我错过了它,我到处找。

更新:

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

4

3 回答 3

1

您可以使用标准Date方法获取号码,getDay或者getUTCday

new Date('2012-10-03').getDay(); // 2
于 2012-10-03T18:58:24.420 回答
1

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

new Date().getDay();
于 2012-10-03T19:00:04.217 回答
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"); 
于 2013-06-15T07:47:21.883 回答