JavaScript setUTCHours返回错误的日期 [英] JavaScript setUTCHours returns wrong day

查看:67
本文介绍了JavaScript setUTCHours返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现setUTCHours()返回错误的一天时,我一直在研究Date()对象。

I've been playing around with the Date() object while studying when I noticed that setUTCHours() returns the wrong day.

示例:

var myDate = new Date(2014, 0, 1);
myDate.setUTCHours(10);
myDate;

为此,我希望日期为UTC Jan 01 2014 10:00:00 UTC,但相反,它落后了一天。为什么?

Looking at this, I expected the date to be Wed Jan 01 2014 10:00:00 UTC, but instead Its one day behind. Why is that?

这是我的 http://jsfiddle.net / L5QEC / 与其他一些基本方法的比较。

Here's my http://jsfiddle.net/L5QEC/ with comparisons to some other basic methods.

推荐答案

日期对象使用 时间值 。他们还具有 偏移量 代表主机系统的时区偏移量。默认情况下,日期和时间将使用偏移量显示本地值。如果您是UTC + 1,则偏移量将为-60,新的 Date(2014,0,1)将为 2013-创建一个日期12-31T23:00:00Z ,然后使用偏移量显示本地日期 2014-01-01T00:00:00 + 0100

Date objects use a time value that is UTC. They also have an offset that represents the timezone offset of the host system. By default, dates and times will use the offset to display local values. If you are UTC+1, then the offset will be -60 and new Date(2014, 0, 1) will create a date for 2013-12-31T23:00:00Z and use the offset to display a local date of 2014-01-01T00:00:00+0100.

因此,如果您将UTC时间更改为10,则UTC时间为: 2013-12-31T10:00:00Z ,而本地等效项为 2013-12-31T11:00:00 + 0100

So if you change the UTC hours to 10, the UTC time is: 2013-12-31T10:00:00Z and the local equivalent is 2013-12-31T11:00:00+0100.

因此,通过设置将UTC小时设置为10,您实际上将前一天的本地时间设置为11:00(即UTC小时+ 1小时偏移)。

So by setting the UTC hours to 10 you effectively set the local time to 11:00 (i.e. the UTC hours + 1 hour offset) on the previous day.

这篇关于JavaScript setUTCHours返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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