Javascript Date.UTC() 函数关闭了一个月? [英] Javascript Date.UTC() function is off by a month?

查看:17
本文介绍了Javascript Date.UTC() 函数关闭了一个月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到这种奇怪的行为时,我正在玩 Javascript 创建一个简单的倒计时时钟:

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 );

剩下的天数是 30 天.

The days left is off by 30 days.

这段代码有什么问题?

我更改了变量名称以使其更清晰.

I changed the variable names to make it more clear.

推荐答案

对于 JavaScript,月份是从零开始的.

The month is zero-based for JavaScript.

日和年以一为单位.

去图.

更新

之所以会这样,来自 JavaScript 的创造者,是

The reason this is so, from the creator of JavaScript, is

JS 必须看起来像 Java",只是少了一点,成为 Java 的愚蠢弟弟或男孩人质伙伴.另外,我必须在十天内完成,否则会发生比 JS 更糟糕的事情.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆