Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 859
user583726 Avatar asked Jan 26 '15 21:01

user583726


1 Answers

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
like image 194
Schwern Avatar answered Nov 12 '22 00:11

Schwern