日期:2014-05-16  浏览次数:20509 次

我想计算输入日期前3天的日期,当月为12月的时候就会将年加1年,月变成0,为什么?
我想计算输入日期前3天的日期,当月为12月的时候就会将年加1年,月变成0,为什么?
代码如下:
<script>
var temp = '2007-12-10'.split('-');
  var d=new Date(temp[0],temp[1],temp[2]); 
  d.setDate(d.getDate()-3);  
alert(d.getFullYear()+'-'+d.getMonth()+"-"+d.getDate());  
var temp = '2007-11-10'.split('-');
  var d=new Date(temp[0],temp[1],temp[2]); 
  d.setDate(d.getDate()-3);  
alert(d.getFullYear()+'-'+d.getMonth()+"-"+d.getDate());  
</script>

------解决方案--------------------
js的月是0-11的
所以你要d.getMonth()+1就可以了
------解决方案--------------------
正解,除了日期是1-31日以外时间基本上都是0开始作为偏移量的
如星期是0-6,月份是0-11,时间点是0-23,分秒钟0-59
等,跟数组一样