JavaScript中有效的日期时间字符串是什么? [英] What are valid Date Time Strings in JavaScript?

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

问题描述

在JavaScript中使用 new Date Date.parse 时,我不能只传递任意日期格式。根据格式,我得到的日期与我想要的不同,甚至无效日期而不是日期对象。某些日期格式在一个浏览器中工作,但在其他浏那么我应该使用哪种日期时间格式?

When using new Date or Date.parse in JavaScript, I cannot just pass arbitrary date formats. Depending on the format, I get a different date than I wanted to or even Invalid Date instead of a date object. Some date formats work in one browser but not in others. So which date time formats should I use?

其他问题:


  • 所有浏览器都支持相同的格式吗? Mozilla Firefox,Google Chrome,Microsoft Internet Explorer,Microsoft Edge和Apple Safari如何处理日期时间字符串? Node.js怎么样?

  • Do all browser support the same formats? How do Mozilla Firefox, Google Chrome, Microsoft Internet Explorer, Microsoft Edge, and Apple Safari handle date time strings? What about Node.js?

是否需要考虑本地日期格式?例如。如果我住在瑞士并且日期格式是30.07.2018,我可以使用新日期('30 .07.2018')

Does it take the local date format into consideration? E.g. if I live in Switzerland and the date format is 30.07.2018, can I use new Date('30.07.2018')?

是否需要考虑当地时区?

Does it take the local time zone into consideration?

如何从日期对象中获取日期时间字符串?

How can I get a date time string from a date object?

如何检测无效的日期时间字符串?

How can I detect invalid date time strings?

日期库如何像Moment一样.js处理日期字符串?

How do date libraries like Moment.js handle date strings?

如果您没有注意到,我回答了我自己的问题(< a href =https://stackoverflow.com/help/self-answer>为什么?)。

推荐答案

Essentials



JavaScript仅正式支持ISO 8601扩展格式的简化。格式如下: YYYY-MM-DDTHH:mm:ss.sssZ 。字母 T 是日期/时间分隔符, Z 是指定为的时区偏移量Z (对于UTC)或 + - 后跟时间表达式 HH:MM
该格式的某些部分(例如时间)可以省略。

The Essentials

JavaScript only officially supports a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ. The letter T is the date/time separator and Z is the time zone offset specified as Z (for UTC) or either + or - followed by a time expression HH:mm. Some parts (e.g. the time) of that format can be omitted.

注意必须至少有四位数,月/日/小时/分钟/秒必须正好有两位数,毫秒必须正好有三位数。例如, 99-1-1 不是有效的日期字符串。

Note that years must have at least four digits, month/day/hours/minutes/seconds must have exactly two digits, and milliseconds must have exactly three digits. For example, 99-1-1 is not a valid date string.

这些是有效日期的一些示例(时间)字符串:

These are some examples of valid date (time) strings:


  • 2018-12-30

  • 2018-12-30T20:59

  • 2018-12-30T20:59 :00

  • 2018-12-30T20:59:00.000Z

  • 2018-12-30T20:59:00.000 + 01:00

  • 2018-12 -30T20:59:00.000-01:00

  • 2018-12-30
  • 2018-12-30T20:59
  • 2018-12-30T20:59:00
  • 2018-12-30T20:59:00.000Z
  • 2018-12-30T20:59:00.000+01:00
  • 2018-12-30T20:59:00.000-01:00

当您省略时区偏移时,日期 - 时间被解释为用户本地时间。
当你完全省略时间时,日期被解释为UTC。

When you omit the time zone offset, date-times are interpreted as user local time. When you omit the time altogether, dates are interpreted as UTC.

重要
所有现代且相当古老浏览器和实现根据规范支持全长日期时间格式。
但是,在没有时区的日期(时间)字符串处理方面存在差异(有关详细信息,请参阅下面的缺少时区偏移)。您应该使用没有时区的日期时间字符串(状态2018)。
而是以毫秒或单独传递 unix时间戳日期不同部分的参数日期构造函数。

Important: All modern and reasonably old browsers and implementations support the full-length date time format according to the specification. However, there are differences in the handling of date (time) strings without a time zone (see "Missing Time Zone Offset" below for details). You should not use date time strings without a time zone (Status 2018). Instead pass a unix timestamp in milliseconds or separate arguments for different parts of the date to the Date constructor.

大多数浏览器也支持其他一些格式但它们未指定,因此不能以相同的方式在所有浏览器中工作。
如果有的话,你应该只使用上面解释的日期时间字符串格式。
其他浏览器甚至在同一浏览器的其他版本中,其他每种格式都可能会中断。

Most browser also support some other formats but they are not specified and thus don't work in all browsers the same way. If at all, you should only use the date time string formats explained above. Every other format might break in other browsers or even in other versions of the same browser.

如果遇到无效日期而不是日期对象,你很可能使用无效的日期时间字符串。

If you run into Invalid Date instead of a date object, you most probably are using an invalid date time string.

和现在更详细一点。

ECMAScript(JavaScript语言实现的规范) )已在 中支持日期字符串新日期 规范)和 Date.parse 规范)因为它的开始。
但是,第一个版本实际上并没有指定日期时间格式。
2009年,当ES5推出日期时间格式规范时,情况发生了变化。

ECMAScript (the specification that the JavaScript language implements) has been supporting date strings in new Date (specification) and Date.parse (specification) since its inception. However, the first versions did not actually specify a date time format. This changed in 2009 when ES5 was introduced with a specification of a date time format.

ECMAScript指定日期时间字符串格式作为 ISO 8601扩展格式简化。格式如下: YYYY-MM-DDTHH:mm:ss.sssZ

ECMAScript specifies the Date Time String Format as a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ.



  • YYYY 是公历格里高利历中0000年至9999年的小数位数。

  • - (连字符)在字符串中显示两次。

  • MM 是从01(1月)到12(12月)的一年中的一个月。

  • DD 是从01开始的月份中的哪一天

  • T 字面上出现在字符串中,表示时间元素的开头。

  • HH 是从午夜起经过的完整小时数,为00到24之间的两位小数。

  • (冒号)在字符串中显示两次。

  • mm 是自小时开始以来的完整分钟数,从00到59的两位小数。

  • ss 是完整秒数从t开始分钟为00到59之间的两位小数。

  • (点)字面上出现在字符串中。

  • sss 是自第二个开始以来的完整毫秒数,为三位十进制数。

  • Z 是指定为Z(对于UTC)或+或 - 后跟时间表达式 HH:mm <的时区偏移量/ code>

  • YYYY is the decimal digits of the year 0000 to 9999 in the proleptic Gregorian calendar.
  • - (hyphen) appears literally twice in the string.
  • MM is the month of the year from 01 (January) to 12 (December).
  • DD is the day of the month from 01 to 31.
  • T appears literally in the string, to indicate the beginning of the time element.
  • HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.
  • : (colon) appears literally twice in the string.
  • mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.
  • ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.
  • . (dot) appears literally in the string.
  • sss is the number of complete milliseconds since the start of the second as three decimal digits.
  • Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression HH:mm

尚未指定其他日期时间格式。
ECMAScript不考虑任何用户本地日期时间格式,这意味着您不能使用国家或地区特定的日期时间格式。

Other date time formats have not been specified. ECMAScript does not take any user local date time formats into account which means that you can not use country or region-specific date time formats.

规范也提及如果字符串不符合[指定的格式,该函数可以回退到任何特定于实现的启发式或特定于实现的日期格式,这可能导致不同浏览器中的不同日期。

The specification also mentions that if "the String does not conform to [the specified] format the function may fall back to any implementation-specific heuristics or implementation-specific date formats" which might result in different dates in different browsers.

规范还包括如下更短的格式。

The specification also includes shorter formats as follows.


这格式包括仅限日期的表格:

This format includes date-only forms:


  • YYYY

  • YYYY-MM

  • YYYY-MM-DD

  • YYYY
  • YYYY-MM
  • YYYY-MM-DD

它还包括日期 - 时间表格,其中包含上述仅限日期的表格之一由以下时间形式之一降低并附加可选的时区偏移量:

It also includes "date-time" forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional time zone offset appended:


  • THH:mm

  • THH:mm:ss

  • THH:mm:ss.sss

  • THH:mm
  • THH:mm:ss
  • THH:mm:ss.sss



后备值



Fallback Values


[...]如果 MM DD 字段不存在01用作值。如果 HH mm ss 字段不存在00用作值,缺席 sss 字段的值为 000\" 。如果没有时区偏移,则仅日期表单将被解释为UTC时间,日期时间表单将被解释为本地时间。

[...] If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

有关缺少浏览器支持的详细信息,请参阅下面的缺少时区偏移。

See "Missing Time Zone Offset" below for more information on the lacking browser support.


格式字符串中的非法值(越界以及语法错误)意味着格式字符串不是此格式的有效实例。

Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.

例如,新日期('2018-01-32')新日期('2018-02-29')将导致无效日期

ECMAScript的日期时间格式也指定延长年份,即六位数年份值。
这种扩展年份字符串格式的示例如 + 287396-10-12T08:59:00.992Z ,表示年份287396 AD
延长年份可以是正数也可以是负数。

The date time format of ECMAScript also specifies extended years which are six digit year values. An example of such an extended year string format looks like +287396-10-12T08:59:00.992Z which denotes a date in the year 287396 A.D. Extended years can either be positive or negative.

ECMAScript指定范围广泛< a href =http://ecma-international.org/ecma-262/9.0/#sec-properties-of-the-date-prototype-object =noreferrer>日期对象属性。
给定有效日期对象,您可以使用 Date.prototype.toISOString() 获取有效的日期时间字符串。
请注意,时区始终为UTC。

ECMAScript specifies a wide range of date object properties. Given a valid date object, you can use Date.prototype.toISOString() to get a valid date time string. Note that the time zone is always UTC.

new Date().toISOString() // "2018-08-05T20:19:50.905Z"

还可以检测日期对象是否有效或使用以下函数无效日期

It is also possible to detect whether a date object valid or an Invalid Date using the following function.

function isValidDate(d) {
  return d instanceof Date && !isNaN(d);
}

来源和更多信息可在在JavaScript中检测无效日期日期实例

以下日期时间格式均有效符合规范,应该适用于支持ES2016或更高版本的每个浏览器,Node.js或其他实现。

The following date time formats are all valid according to the specification and should work in every browser, Node.js or other implementation that supports ES2016 or higher.

2018
2018-01
2018-01-01
2018-01-01T00:00
2018-01-01T00:00:00
2018-01-01T00:00:00.000
2018-01-01T00:00:00.000Z
2018-01-01T00:00:00.000+01:00
2018-01-01T00:00:00.000-01:00
+002018-01-01T00:00:00.000+01:00



无效的日期时间格式



请注意,以下示例根据规范无效。
但是,这并不意味着没有浏览器或其他实现将它们解释为日期。请不要使用下面的任何日期时间格式,因为它们是非标准的,并且在某些浏览器或浏览器版本中可能会失败。

Invalid Date Time Formats

Note that the following examples are invalid as per the specification. However, that does not mean that no browser or other implementation interprets them to a date. Please do not use any of the date time formats below as they are non-standard and might fail in some browsers or browser versions.

2018-1-1 // month and date must be two digits
2018-01-01T0:0:0.0 // hour/minute/second must be two digits, millisecond must be three digits
2018-01-01 00:00 // whitespace must be "T" instead
2018-01-01T00 // shortest time part must have format HH:mm
2018-01-01T00:00:00.000+01 // time zone must have format HH:mm



浏览器支持



今天,每个现代且相当旧的浏览器都支持日期时间格式是在2009年与ES5规范一起引入的。
但是,即使在今天(状态2018),对于没有时区的日期时间字符串也有不同的实现(请参阅下面的缺少时区偏移)。
如果您需要支持旧浏览器或使用没有时区的字符串,则不应使用日期时间字符串
相反,传递自1月以来的毫秒数1,1970,00:00:00 UTC
两个或多个表示 Date 构造函数的不同日期部分的参数。

Browser Support

Today, every modern and reasonably old browser supports the date time format that was introduced with the ES5 specification in 2009. However, even today (Status 2018) there are different implementations for date time strings without a time zone (see "Missing Time Zone Offset" below). If you need to support older browsers or use strings without a time zone, you should not use date time strings. Instead, pass a number of milliseconds since January 1, 1970, 00:00:00 UTC or two or more arguments representing the different date parts to the Date constructor.

ES5.1 错误表明缺少时区偏移的值是Z与ISO 8601相矛盾。
此错误已在 ES6(ES2015)并在 ES2016 (参见下面ECMAScript规范的更改)。
从ES2016开始,没有时区的日期时间字符串被解析为本地时间,而仅日期字符串被解析为UTC。

ES5.1 incorrectly states that the value of an absent time zone offset is "Z" which contradicts with ISO 8601. This mistake was fixed in ES6 (ES2015) and extended on in ES2016 (see "Changes to the ECMAScript Specifications" below). As of ES2016, date time strings without a time zone are parsed as local time while date only strings are parsed as UTC.

根据这个答案,一些实现从未实现过ES5.1中指定的行为。
其中一个似乎是Mozilla Firefox。
其他似乎符合ES2016(及更高版本)规格的浏览器是Google Chrome 65 +,Microsoft Internet Explorer 11和Microsoft Edge。
当前版本的Apple Safari(11.1.2)不符合,因为它错误地分析没有时区的日期时间字符串(例如 2018-01-01T00: 00 )作为UTC而不是当地时间。

According to this answer, some implementations never implemented the behaviour specified in ES5.1. One of them seems to be Mozilla Firefox. Other browsers that seem to be compliant with the specification of ES2016 (and higher) are Google Chrome 65+, Microsoft Internet Explorer 11, and Microsoft Edge. The current version of Apple Safari (11.1.2) is not compliant as it erroneously parses date time strings without a time zone (e.g. 2018-01-01T00:00) as UTC instead of local time.

ES5在2009年引入了日期时间字符串规范。
在此之前,并没有所有浏览器都支持的指定格式。
因此,每个浏览器供应商都添加了对不同格式的支持,这些格式通常不适用于不同的浏览器(和版本)。
有关古代历史的一个小例子,请参阅日期格式

ES5 introduced a specification for date time strings in 2009. Before that, there were no specified formats that were supported by all browsers. As a result, each browser vendor added support for different formats which often did not work accross different browsers (and versions). For a small example of ancient history, see date-formats.

大多数浏览器仍支持这些旧版格式,以免破坏旧网站的向后兼容性。
但依赖那些非标准格式是不安全的,因为它们可能不一致或随时被删除。

Most browsers still support those legacy formats in order to not break the backward compatibility of older websites. But it is not safe to rely on those non-standard formats as they might be inconsistent or get removed at any time.

Node.js正在V8 JavaScript引擎上运行,该引擎也在谷歌浏览器中使用。
所以关于日期时间字符串格式的相同规范适用。
虽然代码在后端运行,但用户本地时间不影响时区而只影响设置在服务器上做。
托管Node.js应用程序的大多数平台即服务(PaaS)提供程序使用UTC作为其默认时区。

Node.js is running on the V8 JavaScript engine which is also used in Google Chrome. So the same specification regarding the date time string format applies. As the code runs in the backend though, the user local time does not influence the time zones but only the settings on the server do. Most platform as a service (PaaS) provider that host Node.js applications use UTC as their default time zone.

当下。 js 是一个非常受欢迎的库,可以帮助您处理JavaScript中的日期,还可以支持比ECMAScript指定的更多格式
此外,Moment.js还支持根据字符串创建日期对象任意格式

Moment.js is a very popular library to help with the handling of dates in JavaScript and it also supports more formats than ECMAScript specifies. In addition, Moment.js also supports creating date objects based on a string and a arbitrary format.

Luxon 支持解析 ISO 8601,HTTP,RFC2822,SQL和任意格式。但仅针对不同的日期时间格式使用不同的函数。

Luxon supports parsing of ISO 8601, HTTP, RFC2822, SQL, and arbitrary formats. But only using different functions for different date time formats.

值得注意的列表有关日期时间字符串格式的ECMAScript规范的更改。

A list of notable changes in the ECMAScript specifications regarding date time string formats.

ES2017 ES2018

Changes in ES2017 and ES2018

没有显着的变化。

ES2016中的更改

Changes in ES2016


如果没有时区偏移,则将日期时间解释为本地时间时间。

当时区偏移不存在时,仅日期表格被解释为UTC时间,日期时间表格被解释为当地时间。

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

ES6(ES2015)

Changes in ES6 (ES2015)


缺席时区偏移的值为Z

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

如果没有时区偏移,日期时间被解释为当地时间。

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

来自更正和澄清在ECMAScript 2015中可能的兼容性影响


如果没有时区偏移,则为当地时间区域使用。版本5.1错误地指出缺少的时区应解释为z

请参阅日期时间字符串格式:与ES5的默认时区差异与Web兼容有关该变更的更多详情。

See Date Time String Format: default time zone difference from ES5 not web-compatible for more details on that change.

ES5.1

Changes in ES5.1


如果 MM DD 字段不存在01用作值。如果 HH mm ss 字段不存在00用作值,缺席 sss 字段的值为 000 。缺席时区偏移的值是Z

If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". The value of an absent time zone offset is "Z".

ES5

Changes in ES5

首次在ECMAScript规范中引入日期时间字符串格式。

First introduction of a date time string format to the ECMAScript specification.


ECMAScript根据ISO 8601扩展格式的简化定义了日期时间的字符串交换格式。格式如下: YYYY-MM-DDTHH:mm:ss.sssZ

ES2 ES3

Changes in ES2, ES3

没有显着变化。
请注意,ES4已被放弃且从未发布。

No notable changes. Note that ES4 was abandoned and never released.

初始规格: ES1

Initial Specification: ES1

ES1引入了日期时间字符串,用于新日期(值) Date.parse(value)
但是,它没有指定实际的日期(时间)格式,它甚至声明

ES1 introduced date time strings to be used in new Date(value) and Date.parse(value). However, it did not specify an actual date (time) format, it even states that


[...]由 Date.parse 生成的值是依赖于实现的[...]

[...] the value produced by Date.parse is implementation dependent [...]

这篇关于JavaScript中有效的日期时间字符串是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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