为什么不是“2”等于“20162-16 00:00” [英] Why isn't "2016-02-16" equal to "2016-02-16 00:00"?

查看:176
本文介绍了为什么不是“2”等于“20162-16 00:00”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个日期字符串传递给 new Date(t)

I'm trying to pass both date strings to new Date(t).

字符串代表同一时间,毕竟如果我省略时间,那不应该是当天的午夜吗?

I expect both strings represent the same time, after all, if I omit the time, shouldn't it be midnight of that day?

但是,

new Date("2016-02-16 00:00")

返回20162-16,午夜,当地时间如预期,

returns 2016-02-16, midnight, local time as expected,

new Date("2016-02-16")

返回UTC2-16,午夜UTC错误,或至少不是我预期的其他字符串解析为什么。

returns 2016-02-16, midnight UTC, which is wrong, or at least not what I expected given what the other string parses as.

如果两者都具有相同的行为,无论是返回,我都会明白时间作为当地时间,或以UTC为准,但是为什么他们返回不同的东西,似乎非常不一致。

I would understand it if they would both have the same behavior, whether it is to return the time as local time, or as UTC, but it seems very inconsistent why they return different things like this.

作为一种解决方法,每当我遇到没有相应的时间戳记我可以附加00:00来获得一致的行为,但似乎这是相当脆弱的。

As a workaround, whenever I encounter a date that has no corresponding timestamp I can append " 00:00" to get consistent behavior, but it seems like this is rather fragile.

我从INPUT元素获取这个值,类型为'datetime-local',所以看起来特别不一致,我必须解决页面元素返回的值。

I am getting this value from an INPUT element, of type 'datetime-local' so it seems especially inconsistent that I have to work around a value returned by a page element.

我做错了,还是应该做不同的事情?

Am I doing something wrong, or should I be doing something differently?

推荐答案

这是什么 ES5.1规范说: / p>

It's what the ES5.1 specification says to do:


缺席时区偏移量的值为Z。

The value of an absent time zone offset is "Z".

它还说:


函数首先尝试根据调用的规则解析String的格式日期时间字符串格式(15.9.1.15)。如果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.

由于格式需要日期和时间之间的 T 分隔符,所以有效时间到UTC:

Since the format requires a T separator between date and time, the valid times go to UTC:

> new Date("2016-02-16T00:00:00")
Tue Feb 16 2016 01:00:00 GMT+0100 (CET)
> new Date("2016-02-16")
Tue Feb 16 2016 01:00:00 GMT+0100 (CET)

...在node.js中,无效的时间(没有T分隔符)似乎去执行特定的本地时间:

...while in node.js, an invalid time (without the T separator) seems to go to the implementation specific localtime:

> new Date("2016-02-16 00:00:00")
Tue Feb 16 2016 00:00:00 GMT+0100 (CET)

请注意,ES6 更改为,在同一个部分文档更改为:

Note that ES6 changed this, in the same part of the documentation it changes to:


如果时区偏移不存在,日期时间将被解释为本地时间。

If the time zone offset is absent, the date-time is interpreted as a local time.

a href =https://bugs.ecmascript.org/show_bug.cgi?id=112 =nofollow> break changes 。

The joy of breaking changes.

根据 TC39 ,该规范旨在被解释为没有时区的日期和时间字符串(例如,2〜16:00:00)被视为本地(根据ISO 8601),但日期只有字符串(例如2016-02-16)为UTC(与ISO 8601不一致)。

According to TC39, the specification is meant to be interpreted as date and time strings without a time zone (e.g. "2016-02-16T00:00:00") are treated as local (per ISO 8601), but date only strings (e.g. "2016-02-16") as UTC (which is inconsistent with ISO 8601).

这篇关于为什么不是“2”等于“20162-16 00:00”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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