为什么同一日期有不同的时间? [英] Why does the same date have different hours?

查看:134
本文介绍了为什么同一日期有不同的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许答案很明显,但我不明白。为什么代码片段中的日期不同?格式还能说几点吗?



  console.log(2017-1-9  - > )console.log(new Date(2017-1-9))console.log(2017-1-09  - >)console.log(new Date(2017-1-09))console .log(2017-01-9  - >)console.log(new Date(2017-01-9))console.log(2017-01-09  - >)console.log (new Date(2017-01-09))console.log(2017-01-11  - >)console.log(new Date(2017-01-11)) 

  .as-console-wrapper {min-height:100%;} code> 



即使在我的Chrome控制台和代码段控制台我得到不同的日期:





我的问题不在于两个都鞋底记录不同的日期。仅在每个控制台中的小时中,在相同的日期,只有 datestring 不同。因此我的问题:



为什么日期不同?



更新< h2>

为什么 new Date(2017-1-1)。getTime()!= new Date(2017-01-01)。 getTime() - > true



  var date = new Date 2017-2-9); var dateWithZero = new Date(2017-02-09); console.log(date); console.log(date.getTime()); console.log(dateWithZero);控制台.log(dateWithZero.getTime()); console.log(date.getTime()== dateWithZero.getTime());  

  .as-console-wrapper {min-height:100%;}  

/ div>

解决方案

TL; DR:因为语言规范说日期字符串不符合可以根据任何实现特定的启发式或实现特定的日期格式分析指定的格式,而 YYYY-MD 只是这样一个字符串。



让我们深入了解规格。以下是 ECMAScript 5.1规范关于Date构造函数(我引用它而不是当前的ES2016)规范只是因为它更简单,但后者基本相同在这种情况下):


15.9.3.2新日期(值)



...



新建的
对象的[[PrimitiveValue]]内部属性设置如下:


  1. 然后




    • v 解析为日期,方式与 parse 方法( 15.9.4.2 );让 V 成为此日期的时间值。


  2. ...



这里是 parse (强调我的):


15.9.4.2 Date.parse(string)



解析函数将ToString运算符应用于其参数,并将生成的字符串解释为一个日期和时间...该函数首先尝试根据日期时间字符串格式( 15.9.1.15 )。 如果String不符合该格式,该函数可能会退回到任何实现特定的启发式或实现特定的日期格式。


日期时间字符串格式,在简单来说,是 YYYY-MM-DDTHH:mm:ss.sssZ 及其子集。由于 YYYY-M-D 不符合该格式,因此(不幸的是)解释器可以随意做任何想要的事情。如果您想知道为什么Chrome以这种特殊方式执行此操作,则必须在V8源中挖掘 / a>。


Maybe the answer is obvious, but I don't get it. Why are the Dates in the Code Snippet different? Does the format say something about the hours as well?

console.log("2017-1-9 -->")
console.log(new Date("2017-1-9"))
console.log("2017-1-09 -->")
console.log(new Date("2017-1-09"))
console.log("2017-01-9 -->")
console.log(new Date("2017-01-9"))
console.log("2017-01-09 -->")
console.log(new Date("2017-01-09"))
console.log("2017-01-11 -->")
console.log(new Date("2017-01-11"))

.as-console-wrapper { 
  min-height: 100%;
}

Even in my chrome-console and the code-snippet-console I get different dates:

My problem is not that both consoles log different dates. Look only at the hours in each console, for the very same date, only the datestring is different. Consequently my question:

Why are the dates different?

UPDATE

Why is new Date("2017-1-1").getTime() != new Date("2017-01-01").getTime() --> true?

var date = new Date("2017-2-9");
var dateWithZero = new Date("2017-02-09");
console.log(date);
console.log(date.getTime());
console.log(dateWithZero);
console.log(dateWithZero.getTime());
console.log(date.getTime() == dateWithZero.getTime());

.as-console-wrapper {
  min-height: 100%;
}

解决方案

TL;DR: Because the language specification says that date strings not conforming to the specified format can be parsed according to "any implementation-specific heuristics or implementation-specific date formats," and YYYY-M-D is just such a string.

Let's dive into the spec. Here's what the ECMAScript 5.1 spec says about the Date constructor (I'm quoting it instead of the current, ES2016 spec just because it's simpler, but the latter works basically the same in this case):

15.9.3.2 new Date (value)

...

The [[PrimitiveValue]] internal property of the newly constructed object is set as follows:

  1. Let v be ToPrimitive(value).
  2. If Type(v) is String, then

    • Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.
  3. ...

And here's the spec for parse (emphasis mine):

15.9.4.2 Date.parse (string)

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time... 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. ...

Date Time String Format, in a nutshell, is YYYY-MM-DDTHH:mm:ss.sssZ and its subsets. Since YYYY-M-D doesn't conform to that format, the interpreter is (unfortunately) free to do whatever it wants. If you want to know why Chrome does it in this particular way, you'll have to dig around in the V8 source.

这篇关于为什么同一日期有不同的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文

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