为什么是yyyyy -MM-DD != YYYY/MM/DD [duplicate]

时间:2022-10-29 13:06:36

This question already has an answer here:

这个问题已经有了答案:

In Chrome, we get some weirdness

在Chrome浏览器中,我们会看到一些奇怪的东西

> new Date("2014-01-01") - new Date("2014/01/01")
< 3600000

And this is because

这是因为

new Date("2014-01-01")
Wed Jan 01 2014 01:00:00 GMT+0100 (CET)

while

new Date("2014/01/01")
Wed Jan 01 2014 00:00:00 GMT+0100 (CET)

Why do the '-' seem to add 1 hour to the time?

为什么“-”似乎会增加一个小时的时间?

3 个解决方案

#1


26  

I believe that the difference is caused by Date.parse adding UTC to one string but not the other, namely: / is not a legal separator in Date.parse() which means that UTC isn't added to the time once it's parsed. Because ' is a legal separator, it is parsed and then UTC is added to the returned time.

我认为差异是由日期造成的。解析将UTC添加到一个字符串,而不是另一个字符串,即:/不是Date.parse()中的合法分隔符,这意味着UTC在解析后不会被添加到时间中。因为'是一个合法的分隔符,它被解析,然后UTC被添加到返回的时间。

Date.parse is used by the new Date() method and its implementation is browser specific, I'm surprised this sort of thing doesn't come up more often.

日期。新Date()方法使用了parse,它的实现是特定于浏览器的,我很惊讶这类东西很少出现。

The specification for Date.parse says:

日期的规范。解析说:

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

根据字符串的内容,可以将字符串解释为本地时间、UTC时间或其他时区中的时间。函数首先尝试根据日期时间字符串格式(15.9.1.15)中调用的规则解析字符串的格式。如果字符串不符合该格式,则函数可能退回到任何特定于实现的启发式或特定于实现的日期格式。

So I'd suggest either adding in a timezone manually before you parse, or discarding the time returned by new Date(), however that could lead to issues around midnight etc. The safest thing would be to see if you can get the date in a more specific format from both systems, with timezone information.

所以我建议手动添加一个时区在解析之前,或丢弃时间返回的新日期(),但是这可能会导致问题午夜等等。最安全的就是看你能不能得到一个更具体的格式的日期从两个系统,与时区信息。

#2


12  

Quoting from V8 source code.

引用V8源代码。

The comments from this function

这个函数的注释。

bool DateParser::Parse(Vector<Char> str,
                       FixedArray* out,
                       UnicodeCache* unicode_cache)

Accept ES5 ISO 8601 date-time-strings or legacy dates compatible with Safari.

接受与Safari兼容的ES5 ISO 8601日期-时间-字符串或遗留日期。

ES5 ISO 8601 dates:

ES5 ISO 8601日期:

[('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]

A string that matches both formats (e.g. 1970-01-01) will be parsed as an ES5 date-time string - which means it will default to UTC time-zone. That's unavoidable if following the ES5 specification.

匹配这两种格式的字符串(例如1970-01-01)将被解析为ES5日期-时间字符串——这意味着它将默认为UTC时区。如果遵循ES5规范,这是不可避免的。

The dash (-) is correct notation for Date.

破折号(-)是正确的日期标记。

#3


1  

It is because of globalization. The dash ( - ) is not an English notation (GMT). Javascript parses the notation. Try setting the culture and then use the dash notation.

这是因为全球化。破折号(-)不是英文表示法(GMT)。Javascript解析符号。尝试设置文化,然后使用dash标记。

#1


26  

I believe that the difference is caused by Date.parse adding UTC to one string but not the other, namely: / is not a legal separator in Date.parse() which means that UTC isn't added to the time once it's parsed. Because ' is a legal separator, it is parsed and then UTC is added to the returned time.

我认为差异是由日期造成的。解析将UTC添加到一个字符串,而不是另一个字符串,即:/不是Date.parse()中的合法分隔符,这意味着UTC在解析后不会被添加到时间中。因为'是一个合法的分隔符,它被解析,然后UTC被添加到返回的时间。

Date.parse is used by the new Date() method and its implementation is browser specific, I'm surprised this sort of thing doesn't come up more often.

日期。新Date()方法使用了parse,它的实现是特定于浏览器的,我很惊讶这类东西很少出现。

The specification for Date.parse says:

日期的规范。解析说:

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

根据字符串的内容,可以将字符串解释为本地时间、UTC时间或其他时区中的时间。函数首先尝试根据日期时间字符串格式(15.9.1.15)中调用的规则解析字符串的格式。如果字符串不符合该格式,则函数可能退回到任何特定于实现的启发式或特定于实现的日期格式。

So I'd suggest either adding in a timezone manually before you parse, or discarding the time returned by new Date(), however that could lead to issues around midnight etc. The safest thing would be to see if you can get the date in a more specific format from both systems, with timezone information.

所以我建议手动添加一个时区在解析之前,或丢弃时间返回的新日期(),但是这可能会导致问题午夜等等。最安全的就是看你能不能得到一个更具体的格式的日期从两个系统,与时区信息。

#2


12  

Quoting from V8 source code.

引用V8源代码。

The comments from this function

这个函数的注释。

bool DateParser::Parse(Vector<Char> str,
                       FixedArray* out,
                       UnicodeCache* unicode_cache)

Accept ES5 ISO 8601 date-time-strings or legacy dates compatible with Safari.

接受与Safari兼容的ES5 ISO 8601日期-时间-字符串或遗留日期。

ES5 ISO 8601 dates:

ES5 ISO 8601日期:

[('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]

A string that matches both formats (e.g. 1970-01-01) will be parsed as an ES5 date-time string - which means it will default to UTC time-zone. That's unavoidable if following the ES5 specification.

匹配这两种格式的字符串(例如1970-01-01)将被解析为ES5日期-时间字符串——这意味着它将默认为UTC时区。如果遵循ES5规范,这是不可避免的。

The dash (-) is correct notation for Date.

破折号(-)是正确的日期标记。

#3


1  

It is because of globalization. The dash ( - ) is not an English notation (GMT). Javascript parses the notation. Try setting the culture and then use the dash notation.

这是因为全球化。破折号(-)不是英文表示法(GMT)。Javascript解析符号。尝试设置文化,然后使用dash标记。