Javascript Date.UTC()函数关闭一个月? [英] Javascript Date.UTC() function is off by a month?
问题描述
var a = new,我在玩JavaScript,创建一个简单的倒计时时钟Date(),
now = a.getTime(),
then = Date.UTC(2009,10,31),
diff = then - now,
daysleft = parseInt (diff /(24 * 60 * 60 * 1000));
console.log(daysleft);
剩下的时间是30天前关闭。
此代码有什么问题?
编辑:我更改了变量名,使之更加清晰。
这个月是基于零的JavaScript。
年份为一个。
Go figure。
更新 p>
原因是这样的,从 JavaScript的创建者,是
JS不得不像Java那样看起来像Java的笨蛋兄弟或男孩人质的sidekick。此外,我必须在十天内完成,或者比JS更糟糕的事情。
http://www.jwz.org/ blog / 2010/10 / every-day-i-learn-something-new-and-stupid /#comment-1021
I was playing around with Javascript creating a simple countdown clock when I came across this strange behavior:
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 );
The days left is off by 30 days.
What is wrong with this code?
Edit: I changed the variable names to make it more clear.
The month is zero-based for JavaScript.
Days and years are one-based.
Go figure.
UPDATE
The reason this is so, from the creator of JavaScript, is
JS had to "look like Java" only less so, be Java's dumb kid brother or boy-hostage sidekick. Plus, I had to be done in ten days or something worse than JS would have happened.
http://www.jwz.org/blog/2010/10/every-day-i-learn-something-new-and-stupid/#comment-1021
这篇关于Javascript Date.UTC()函数关闭一个月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!