受支持的未记录Date.parse格式? [英] undocumented supported Date.parse format?

查看:31
本文介绍了受支持的未记录Date.parse格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MDN的 Date.parse 文档说:

参数

dateString 表示 RFC822 ISO 8601 日期的字符串.

parse方法采用日期字符串(例如"1995年12月25日"),并返回自1970年1月1日UTC 00:00:00以来的毫秒数.本地时区用于解释不包含时区信息的参数.此功能对于基于字符串值设置日期值很有用,例如,结合setTime方法和Date对象.

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The local time zone is used to interpret arguments that do not contain time zone information. This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.

给出一个表示时间的字符串,parse返回时间值.它接受RFC822/IETF日期语法(RFC 1123第5.2.14节和其他地方),例如"1995年12月25日星期一13:30:00 GMT" .它可以理解美国大陆时区的缩写,但是对于一般用途,请使用时区偏移量,例如"1995年12月25日星期一格林尼治标准时间+0430"(格林尼治以东4小时30分钟)子午线).如果您未指定时区,则采用本地时区.GMT和UTC被认为是等效的.

Given a string representing a time, parse returns the time value. It accepts the RFC822 / IETF date syntax (RFC 1123 Section 5.2.14 and elsewhere), e.g. "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

或者,日期/时间字符串可以采用ISO 8601格式.从JavaScript 1.8.5/Firefox 4开始,支持ISO 8601的子集.例如,可以传递和解析"2011-10-10"(日期)或"2011-10-10T14:48:00(日期和时间).ISO日期中的时区尚未确定支持,因此"2011-10-10T14:48:00 + 0200"(带有时区)尚未提供预期的结果.

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed. Timezones in ISO dates are not yet supported, so e.g. "2011-10-10T14:48:00+0200" (with timezone) does not give the intended result yet.

但是:

var t = new Date("4/25/2010");
console.log(t);

// Output: Sun Apr 25 2010 00:00:00 GMT+0100 (GMT Daylight Time)

在何处写明应支持 MM/dd/yyyy 格式?

推荐答案

不是.规范中概述了基线要求,但允许实现支持他们选择的其他格式.

It isn't. The baseline requirements are outlined in the spec but implementations are allowed to support additional formats of their choosing.

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

显然,这些格式可以相差很大,并且某些常用格式是模棱两可的,例如"1/2/1970".可能是2月1日或1月2日.如果要编写跨平台代码,请仅依赖15.9.1.15中的基线格式.

Obviously, these can vary widely, and some commonly used formats are ambiguous such as "1/2/1970" which might be the first of February or the second of January. If you want to write cross-platform code, only rely on the baseline formats in 15.9.1.15.

关于YYYY-MM-DD和YYYY/MM/DD之间的差异,第一个被指定为以零时间返回该日期,而第二个未指定,因此允许实现产生喜欢的任何日期.sepc的相关部分是

Re the difference between YYYY-MM-DD and YYYY/MM/DD, the first is speced as returning that date with a zero time, whereas the second is not specified so an implementation is allowed to produce any date it likes. The relevant portion of the sepc is

此格式包括仅日期格式:

This format includes date-only forms:

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

...

所有数字都必须以10为底.如果缺少MM或DD字段,则将"01"用作值.如果不存在mm或ss字段,则将"00"用作值,将不存在的sss文件的值设为"000".缺少时区偏移的值为"Z".

All numbers must be base 10. If the MM or DD fields are absent "01" is used as the value. If the mm or ss fields are absent "00" is used as the value and the value of an absent sss file is "000". The value of an absent time zone offset is "Z".

这篇关于受支持的未记录Date.parse格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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