简体   繁体   English

为什么在年末字符串末尾添加空格字符会导致JavaScript(节点)应用时区偏移量?

[英]Why does adding a space character at the end of a year string cause JavaScript (node) to apply the timezone offset?

This is in Node v8.9.0 specifically. 具体来说,这是在Node v8.9.0中。

Consider these lines: 考虑以下几点:

console.log(new Date("2006"));
console.log(new Date("2006 "));

They produce this output: 他们产生这个输出:

2006-01-01T00:00:00.000Z
2006-01-01T08:00:00.000Z

Note that the second line has an 8hr timezone offset. 请注意,第二行具有8小时的时区偏移。

Why would the presence or absence of a trailing space cause the date to be created with/without the timezone offset? 为什么存在或不存在尾随空格会导致创建日期时是否有时区偏移?

Summary: The presence of the space causes the date parser to interpret the date as a different format which uses a different assumption for the time zone when there is no timezone specification in the string. 简介:空间的存在导致日期解析器将日期解释为不同的格式,当字符串中没有时区规范时,该格式对时区使用不同的假设。

A complication with the Date() constructor or Date.parse() is that with incomplete or partial date strings the parser is trying to guess whether the string should be interpreted as a simplified form of the ISO 8601 format or the RFC2822 format . Date()构造函数或Date.parse()的复杂性是,对于不完整或部分日期字符串,解析器试图猜测该字符串是否应被解释为ISO 8601格式RFC2822格式的简化形式。 If it thinks the format is ISO 8601 and no timezone is specified, then the UTC time zone will be assumed. 如果它认为格式为ISO 8601且未指定时区,则将假定UTC时区。 If it thinks the format is RFC2822, then the local computer time zone will be assumed. 如果它认为格式是RFC2822,那么将假定本地计算机时区。

So, for your two strings the first is apparently being assumed to be ISO 8601 and thus uses UTC time zone assumption and the second is apparently being assumed to be RFC2822 which uses the local time zone. 因此,对于您的两个字符串,第一个显然被假定为ISO 8601,因此使用UTC时区假设,第二个显然被假定为使用本地时区的RFC2822。

The ISO 8601 format that is supported contains no spaces, whereas the RFC2822 format can contain some spaces so it seems likely that the mere presence of the space in the string causes the parser to choose the RFC2822 format which uses the local time zone. 支持的ISO 8601格式不包含空格,而RFC2822格式可以包含一些空格,因此字符串中空间的存在似乎可能导致解析器选择使用本地时区的RFC2822格式。

You can read about some of this in the Date.parse() doc on MDN . 您可以在MDN上的Date.parse() doc中阅读其中的一些内容。

For reference here's a piece of the simplified ISO 8601 format: 这里参考的是一个简化的ISO 8601格式:

   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)
     T    = a literal T separating the time

You can see that there are no spaces in this format. 您可以看到此格式中没有空格。


Whereas the RFC2822 format contains spaces separate different pieces of the date/time. RFC2822格式包含空格,分隔日期/时间的不同部分。 The full grammar is in the linked RFC, but here's one example where you can see the spaces: 完整的语法在链接的RFC中,但是这里有一个例子,你可以看到空格:

Mon, 25 Dec 1995 13:30:00 GMT

Note that the ES2015 spec for Date.parse() describes the simplified ISO 8601 date format . 请注意, Date.parse()ES2015规范描述了简化的ISO 8601日期格式 When you supply any string that does not match that format exactly you probably run into some implementation dependent parsing behavior. 当您提供任何与该格式不完全匹配的字符串时,您可能会遇到一些依赖于实现的解析行为。 Quoting from the ES2015 spec: 引自ES2015规范:

the value produced by Date.parse is implementation-dependent when given any String value that does not conform to the Date Time String Format ( 20.3.1.16 ) 当给定任何不符合日期时间字符串格式的字符串值时,Date.parse生成的值与实现有关( 20.3.1.16

where that referenced "Date Time String Format" is the simplified ISO 8601 format. 引用“日期时间字符串格式”的地方是简化的ISO 8601格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM