Javascript getMonth getUTCMonth差分结果
我在使用JavaScript date.getMonth()和date.getUTCMonth()时发现了不一致的结果,但只对某些日期使用。以下示例演示了该问题:Javascript getMonth getUTCMonth差分结果,javascript,Javascript,我在使用JavaScript date.getMonth()和date.getUTCMonth()时发现了不一致的结果,但只对某些日期使用。以下示例演示了该问题: <!DOCTYPE html> <html> <body onload="myFunction()"> <p id="demo">Click the button to display the month</p> <script type="text/javascr
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<p id="demo">Click the button to display the month</p>
<script type="text/javascript">
function myFunction()
{
var d = new Date(2012, 8, 1);
var x = document.getElementById("demo");
x.innerHTML=d;
x.innerHTML+='<br/>result: ' + d.getMonth();
x.innerHTML+='<br/>result UTC: ' + d.getUTCMonth();
}
</script>
</body>
</html>
如果我将日期更改为(2012,2,1),则输出为:
Thu Mar 01 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
result: 2
result UTC: 2
在第一个示例中,getMonth返回7,getUTCMonth返回8。在第二个示例中,两者都返回相同的值2
有没有人已经经历过这种情况?我来自葡萄牙,我认为这与我的GMT有关,但我不明白为什么会发生这种情况,因为这些例子都是在同样的情况下运行的
感谢进步您会发现这是由DST差异造成的。通用时区日期方法用于处理UTC日期 日期返回0到11之间的月份 新日期(1976年1月18日)— 1976年2月18日星期三00:00:00 GMT+0530(印度标准时间) *getUTCDate返回值与getDate()相同,但返回基于世界时区的日期,与月份和年份相同 新日期(1976年1月18日) 十七, 新日期(1976年1月18日) 十八 新日期(1976年2月18日) 二, 新日期(1976年1月18日)- 一, 新日期(1976年1月18日)。getYear() 76 新日期(1976年1月18日) 1976年 新日期(1976年1月18日)。getFullYear() 1976年
Thu Mar 01 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
result: 2
result UTC: 2