Parse JSON Date String (ISO8601)

I can create a JavaScript date object using:

var d=new Date('2012-08-07T07:47:46Z');
document.write(d);

This will record the date using the browser time zone. But I have to be able to (no "Z"):

var d=new Date('2012-08-07T07:47:46');
document.write(d);

Returns the same as above, but in accordance with the ISO8601 standard, a string without a time zone (for example, +01: 00) and without a "Z", the date should be considered in the local time zone. So, the second example above should write a date-time as 7:47 a.m.

I get the datetime string from the server, and I want to display exactly that datetime. Any ideas?

+5
source share
2 answers

I found this script to work well. It extends the Date.parse method.

https://github.com/csnover/js-iso8601/

Date.parse('2012-08-07T07:47:46');

However, it does not work with the constructor new Date().

+6

, Javascript ISO8601.

:

function ISODateString(d) {
  function pad(n){
    return n<10 ? '0'+n : n
  }
  return d.getUTCFullYear()+'-'
  + pad(d.getUTCMonth()+1)+'-'
  + pad(d.getUTCDate())+'T'
  + pad(d.getUTCHours())+':'
  + pad(d.getUTCMinutes())+':'
  + pad(d.getUTCSeconds())+'Z'
}
var d = new Date();
print(ISODateString(d));

: Mozilla

-1

All Articles