Javascript日期对象总是有一天关闭? [英] Javascript date object always one day off?

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

问题描述

  2011-09-24 $在我的java脚本应用程序中,我的日期存储格式如下: b $ b  

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

  var doo = new Date(2011-09-24); 
console.log(doo);
Fri September 23 2011 20:00:00 GMT-0400(Eastern Daylight Time)

任何帮助,请谅解。



谢谢

解决方案

东方夏令时间为 -4小时,而您回来的时间是 20 。 / p>

  20h + 4h = 24h 

这是2011-09-24的午夜。



你得到正确的日期,你从来没有指定正确的时区。 / p>

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

  var d,
days;
d = new Date('2011-09-24');
days = ['太阳','星期一','星期二','星期三','星期四','星期五','星期六'];
console.log(days [d.getUTCDay()]);


In my java-script app I have the date stored in a format like so:

2011-09-24

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)

Any help on this is appreciated.

Thank you

解决方案

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

20h + 4h = 24h

which is midnight of 2011-09-24.

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

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天全站免登陆