当我遇到这种奇怪的行为时,我正在玩Javascript创建一个简单的倒计时时钟:var a = new Date(), now = a.getTime(),then = Date.UTC(2009,10,31),diff = then - now,daysleft = parseInt(diff/(24*60*60*1000));console.log(daysleft );剩下的日子将减少30天。此代码有什么问题?编辑:我更改了变量名称,使其更清晰。
3 回答

慕桂英546537
TA贡献1848条经验 获得超10个赞
正如Eric所说,这是由于几个月被列为0-11范围。
这是一种常见的行为-localtime()和其他许多语言的Perl结果也是如此。
这很可能最初是从Unix的localtime()调用继承的。(做“本地人”)
原因是天/年是它们自己的整数,而月(作为#)是数组的索引,在大多数语言中,尤其是在Unix上实现底层调用的C语言中,其起始位置为0。

慕雪6442864
TA贡献1812条经验 获得超5个赞
date1 = new Date();
//year, month, day [, hrs] [, min] [, sec]
date1 = new Date.UTC(date1.getFullYear(),date1.getMonth()+1,date1.getDate(),date1.getHours(),date1.getMinutes(),date1.getSeconds());
date2 = new Date();
date2 = date2.getTime();
alert(date1)
alert(date2)
添加回答
举报
0/150
提交
取消