最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - JavaScript setUTCHours returns wrong day - Stack Overflow

programmeradmin3浏览0评论

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

Example:

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

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?

Here's my / with parisons to some other basic methods.

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

Example:

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

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?

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

Share Improve this question asked Apr 24, 2014 at 23:59 Peter SilverPeter Silver 631 silver badge3 bronze badges 2
  • 2 Which time zone are you in? When you create the initial date object it is in UTC relative to your local timezone, so if you're ahead of UTC it would go back to the previous day, and then when you set the hours it'll be the hours of that previous day. – jraede Commented Apr 25, 2014 at 0:01
  • Thanks @jraede ! Yes my timezone is UTC+1. I just changed my puters timezone to UTC-1 to check and now it displays the correct day. – Peter Silver Commented Apr 25, 2014 at 0:26
Add a ment  | 

5 Answers 5

Reset to default 11

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.

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.

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.

If you'd like to set a specific date and time in UTC, consider:

var dt = new Date(Date.UTC(2014, 0, 1, 10, 0, 0));

The result will represent that point in universal time, but you will see it adjusted to the local time zone for display. For example:

"Wed Jan 01 2014 02:00:00 GMT-0800 (Pacific Standard Time)"

Read the UTC date, not the local (default) Date:

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

One dirty way is to convert the local date to UTC before you use setUTCHours

function UTCDate() {
    var dateObject = new Date();
    var UTC = new Date(dateObject.getUTCFullYear(),
              dateObject.getUTCMonth(), dateObject.getUTCDate(),
              dateObject.getUTCHours(), dateObject.getUTCMinutes(),
              dateObject.getUTCSeconds(),dateObject.getUTCMilliseconds());
   return UTC;
}

When the time is 0 hours and 0 minutes, there's this annoying one-day delay based on the GMT.

For example if the GMT is +2, the number of hours of the date must be greater than 2 otherwise the date takes one day less.

The solution I found to avoid this weird effect is to use this trick.

var myDate = new Date(2014, 0, 1);
var timezoneOffset = myDate.getTimezoneOffset();
if (timezoneOffset > 0) {
  myDate.setMinutes((24 * 60) - (timezoneOffset + 1));
} else {
  myDate.setMinutes(-timezoneOffset); // Do not forget the negative sign !
}
myDate.setUTCHours(10);

So when I add the time corresponding to the GMT offset I no longer have this wrong date !

I tested all GMTs by changing the settings of my operating system (-13, -2, +2, +12)

You can set the UTCMinutes after that if you want !

The getTimezoneOffset() method returns the time difference between UTC time and local time, in minutes. For example, If your time zone is GMT+2, -120 will be returned.

发布评论

评论列表(0)

  1. 暂无评论