setMonth(1)给了我三月? [英] setMonth(1) gives me March?

查看:129
本文介绍了setMonth(1)给了我三月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么setMonth(1)给我三月?我相信0 = Jan,1 = Feb,2 = Mar

Why is setMonth(1) giving me March? I believe that 0=Jan, 1=Feb, 2=Mar

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = init;

function init(){
var d = new Date();
d.setFullYear(2014);
d.setMonth(1);
d.setDate(1);

document.getElementById("demo").innerHTML = d;
}
</script>
</head>
<body>

<div id="demo"></div>

</body>
</html>

我得到......

周六Mar 01 2014 15:11:03 GMT-0600(中央标准时间)

Sat Mar 01 2014 15:11:03 GMT-0600 (Central Standard Time)

我正在运行Win7 Pro 64位,时钟和日历似乎是正确的。

I'm running Win7 Pro 64-bit and the clock and calendar seem to be correct.

推荐答案

今天是1月31日。当您 d.setMonth(1); 时,您试图将日期设置为2月31日。由于这个日期不存在,它将落到3月3日。

Today is the 31st of January. When you d.setMonth(1); you are trying to set the date to the 31st of February. Since this date doesn't exist, it falls over to the 3rd of March.

设置初始化对象时的整个日期,不要尝试零碎地更改它。

Set the whole date when you initialise the object, don't try to change it piecemeal.

这篇关于setMonth(1)给了我三月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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