JavaScript日期为什么日期新日期(“2011-12-13”)被认为是星期一而不是星期二? [英] javascript Date why is the Date new Date("2011-12-13") considered a monday and not a tuesday?

查看:102
本文介绍了JavaScript日期为什么日期新日期(“2011-12-13”)被认为是星期一而不是星期二?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日期对象实例化如下:

new Date("2011-12-13")

返回一个周一认为自己的日期对象:

return a date object that consider themselves a Monday:

Date {Mon Dec 12 2011 16:00:00 GMT-0800 (PST)}

但根据我的日历,2011年12月13日是星期二。奇怪的是,当我实例化这样的日期对象:

But according to my calendar, 12/13/2011 is a Tuesday. Oddly, when I instantiate the date object like this:

new Date("12/13/2011")

此(正确)返回星期二:

This (correctly) returns a Tuesday:

Date {Tue Dec 13 2011 00:00:00 GMT-0800 (PST)}

如何在初始示例(年月日)中使用语法,并根据我的日历获取一周中的正确日期?

How can I use the syntax in my initial example (year-month-day) and also get the correct day of the week according to my calendar?

推荐答案

您可能需要查看 http://stackoverflow.com/a/163584/436641 在使用字符串的JavaScript中创建Date对象时需要注意。简而言之,为了可靠性,您应该这样做:

You might want to check out http://stackoverflow.com/a/163584/436641 for some caveats on creating Date objects in JavaScript using strings. In a nutshell, for reliability, you should be doing this:

new Date(2011, 11, 13);

(请注意,第二个参数,月份为1月至11月为12月,而不是1通过12。)

(Note that the second parameter, the month, is 0 for January through 11 for December, not 1 through 12.)

在特定情况下,当您使用2011-12-13 GMT,然后根据您的当地时区进行调整,在您的情况下,格林尼治标准时间落后8小时。所以你在询问前一天下午4点。那是星期一,而不是星期二。 (在结果中看到时间是16:00:00,日期是第12个而不是第13个?)

In your particular case, when you instantiate with "2011-12-13" it uses GMT and then adjusts to your local timezone, which in your case is eight hours behind GMT. So you are getting 4pm on the day before you are asking. So that's a Monday, not a Tuesday. (See in the result where it says the time is 16:00:00 and the date is the 12th rather than the 13th?)

当您使用12/13/2011,您当地时区的十三号午夜。所以你得到星期二,你要求的那一天。

When you instantiate with "12/13/2011", you get midnight on the 13th in your local timezone. So you get Tuesday, the day you requested.

区别是(可能)由 Date 将这个字符串传递给它的静态 parse()方法,这可能是(可能)将这些字符串中的一个作为ISO 8601时间戳处理,另一个作为RFC 822时间戳,这些时间戳格式的时区的默认值/最佳猜测是不同的。请参阅 https://developer.mozilla.org/en/JavaScript/Reference/ Global_Objects / Date / parse 了解Firefox的情况。其他浏览器可能也可能不一样。这就是为什么最好不要使用字符串(如果你使用字符串,使用长的明确的标准时间戳格式而不是截断的格式)。

The difference is (probably) explained by the fact that Date will pass the string to its static parse() method, which is (probably) treating one of those strings as an ISO 8601 timestamp and the other as an RFC 822 timestamp, and that the defaults/best-guesses for timezones for those timestamp formats are different. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse for the situation with Firefox. Other browsers may or may not be the same. That's why it's best to not use strings (and if you do use strings, use long unambiguous standard timestamp formats rather than truncated ones).

这篇关于JavaScript日期为什么日期新日期(“2011-12-13”)被认为是星期一而不是星期二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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