月值与实际月份不对应

时间:2016-02-23 20:28:07

标签: javascript jquery

jQuery(document).ready( function($){
    var newDate = new Date( 2016 , 2 , 28 , 8 , 0 , 0 , 0 );
    alert( $.fn.jquery );
    $('#countdown-ex1').countdown({until: newDate});
});

结果:

enter image description here

其中

var newDate = new Date( 2016 , 1 , 28 , 8 , 0 , 0 , 0 );

结果:

enter image description here

jQuery版本:1.11.3

TL; DR;

第2个月等于3月

第1个月等于2月

现在这是一个错误还是故意的? 为什么会故意这样做?

3 个答案:

答案 0 :(得分:0)

月值基于零,即。 0 = Jan,1 = 2月,依此类推。要获得您期望的日期,只需将Month值减少1:

jQuery(document).ready( function($){
    var newDate = new Date( 2016 , 1 , 28 , 8 , 0 , 0 , 0 ); // 28 Feb 2016
    alert( $.fn.jquery );
    $('#countdown-ex1').countdown({until: newDate});
});

答案 1 :(得分:0)

阅读JS Date对象的一些文档:

http://www.w3schools.com/jsref/jsref_obj_date.asp

特别是几个月的时间是0到11 ..

答案 2 :(得分:-1)

日期的月份是0基数。 http://www.w3schools.com/jsref/jsref_getmonth.asp

  

getMonth()方法根据当地时间返回指定日期的月份(从0到11)。