Javascript日期对象总是休息一天吗? [英] Is the Javascript date object always one day off?

查看:143
本文介绍了Javascript日期对象总是休息一天吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java Script应用程序中,我将日期存储为如下格式:

In my Java Script app I have the date stored in a format like so:

2011-09-24

现在,当我尝试使用上面的值来创建一个新的Date对象时(所以我可以检索一个日期)不同的格式),日期总有一天休息。见下文:

Now when I try using the above value to create a new Date object (so I can retrieve the date in a different format), the date always comes back one day off. See below:

var doo = new Date("2011-09-24");
console.log(doo);

日志:

Fri Sep 23 2011 20:00:00 GMT-0400 (Eastern Daylight Time)


推荐答案

请注意,东部夏令时 -4小时,并且您返回的日期 20

Notice that Eastern Daylight Time is -4 hours and that the hours on the date you're getting back are 20.

20h + 4h = 24h

这是2011-09-24的午夜。

which is midnight of 2011-09-24.

你是对的日期,您从未指定正确的时区。

You're getting the right date, you just never specified the correct time zone.

如果您需要访问日期值,您可以使用 getUTCDate()任何其他 getUTC *()函数

If you need to access the date values, you can use getUTCDate() or any of the other getUTC*() functions:

var d,
    days;
d = new Date('2011-09-24');
days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
console.log(days[d.getUTCDay()]);

这篇关于Javascript日期对象总是休息一天吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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