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

2

10

I am working on REST calls where I need to send date in "2006-09-01T07:00:00.000+0000" format.

User enters date in "YYYY/MM/DD" format and I am using "moment.js" to format the date in ISO 8601 using this format "YYYY-MM-DDTHH:MM:SS.MMMZ" . But it gives me this output "1969-06-20T00:06:00.Jun-07:00".

So, how do I get date in this format "2006-09-01T07:00:00.000+0000" using moment.js OR Javascript ?

Spirogyra answered 26/1, 2015 at 21:24 Comment(2)
possible duplicate of How do I output an ISO-8601 formatted string in Javascript?Skilled
Uppercase M is for months and lowercase m is for minutes. You should check the required case for other parts of it too.Abase
S
15

Yes, it is ISO 8601. 2006-09-01T07:00:00.000+0000 is the first day of the ninth month of the year 2006, 7 hours, 0 minutes, 0.000 seconds offset 0 hours from UTC. Whether or not decimals are allowed is up to the parties exchanging dates (which is a fancy way of ISO saying "it's optional").

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.

(As pointed out by @chansen, technically it should be 2006-09-01T07:00:00.000+00:00 with a separator on the time zone because according to 4.3.3(d) every part must use either the basic format (no separators) or the extended format (with separators), but nobody bothers with that, strptime can't produce that format, and you'll probably break some ISO 8601 parsers).

Outputting this is covered in other answers for both Javascript and moment.js.

Javascript has 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).

Here's the moment.js docs on String formatting. YYYY-MM-DDTHH:MM:SS.MMMZ is incorrect because you're using M to mean three different things. What you want is YYYY-MM-DDTHH:mm:ss.SSSZZ.

  • YYYY - year
  • MM - month number (2 digit)
  • DD - day of month (2 digit)
  • HH - hours (2 digit, 24 hour format)
  • mm - minutes (2 digit)
  • ss - seconds (2 digit)
  • SSS - thousands of seconds
  • ZZ - UTC offset
Skilled answered 26/1, 2015 at 21:40 Comment(2)
Actually, it's an invalid ISO 8601 representation. The zone designator can't be in basic format when the date and time is in extended format.Cauliflower
@Cauliflower 4.3.3(d), there it is. You are technically correct (the best kind of correct)! I don't think it matters in practice, everyone screws that one up. I don't think you can even get strptime to output the time zone with a separator.Skilled
S
0

The format that worked for me for 2006-09-01T07:00:00.000+0000

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

Styles answered 10/4, 2017 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.