这是什么日期格式? 2006-09-01T07:00:00.000+0000 ?是 ISO_8601 吗?

what's this date format ? 2006-09-01T07:00:00.000+0000 ? is it ISO_8601 ?

我正在处理 REST 调用,我需要以“2006-09-01T07:00:00.000+0000”格式发送日期。

用户以 "YYYY/MM/DD" 格式输入日期,我正在使用 "moment.js" 使用此格式 "YYYY-MM-DDTHH:MM:SS.MMMZ" 来格式化 ISO 8601 中的日期。但它给了我这个输出“1969-06-20T00:06:00.Jun-07:00”。

那么,如何使用 moment.js 或 Javascript 以这种格式“2006-09-01T07:00:00.000+0000”获取日期?

Yes, it is ISO 86012006-09-01T07:00:00.000+0000 是 2006 年第九个月的第一天,7 小时 0 分钟 0.000 秒从 UTC 偏移 0 小时。是否允许使用小数取决于双方交换日期(这是 ISO 说“它是可选的”的一种奇特方式)。

4.2.2.4 Representations with decimal fraction

If necessary for a particular application a decimal fraction of hour, minute or second may be included. If a decimal fraction is included, lower order time elements (if any) shall be omitted and the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign. If the magnitude of the number is less than unity, the decimal sign shall be preceded by two zeros in accordance with 3.6.

The interchange parties, dependent upon the application, shall agree the number of digits in the decimal fraction. The format shall be [hhmmss,ss], [hhmm,mm] or [hh,hh] as appropriate (hour minute second, hour minute, and hour, respectively), with as many digits as necessary following the decimal sign. A decimal fraction shall have at least one digit. In the examples below it has been agreed to give the smallest time element a decimal fraction with one digit.

(正如@chansen 所指出的,技术上 它应该是 2006-09-01T07:00:00.000+00:00 时区分隔符,因为根据 4.3.3(d) 每个部分必须使用基本格式(无分隔符)或扩展格式(带分隔符),但没有人为此烦恼,strptime 不能生成那种格式,你可能会 break 一些ISO 8601 解析器)。

Javascript and moment.js.

的其他答案中涵盖了输出此内容

Javascript 有 Date.toISOString for output. Date.new will also accept an ISO 8601 string. Every recent browser should support it, though Internet Explorer only added it in version 9 (IE 8 still represents 5% of desktop users).

这是 moment.js docs on String formattingYYYY-MM-DDTHH:MM:SS.MMMZ 不正确,因为您使用 M 表示三个不同的事物。你要的是YYYY-MM-DDTHH:mm:ss.SSSZZ.

  • YYYY - 年
  • MM - 月数(2 位)
  • DD - 月中的第几天(2 位数)
  • HH - 小时(2 位数字,24 小时格式)
  • mm - 分钟(2 位数)
  • ss - 秒(2 位数)
  • SSS - 千秒
  • ZZ - UTC 偏移量

适用于 2006-09-01T07:00:00.000+0000 的格式

是:yyyy-MM-dd'T'hh:mm:ss.sssZ