Question

How can one parse this line into a good date in javascript?

2013-12-19T06:59:27.888+3:00

I think the main problem is the milliseconds. I'm hoping to use moment.js to parse the date into a nicer format as well, I thought it could parse unix style timestamps, but couldn't get it to work with these types.

moment("2013-12-19T06:59:27.888+3:00").format("Do MM YYYY");

Était-ce utile?

La solution

That's a (one of the many) valid ISO 8601 format.

Decimal fractions may be added to any of the three time elements. However, a fraction may only be added to the lowest order time element in the representation..

If momentjs doesn't accept it as ISO 8601 by default (the fractional seconds + offset format is not listed in "supported across all browsers"1), the format is easy to manually specify :

var fmt = "YYYY-MM-DDThh:mm:ss.SSSZZ";
var d = moment(str, fmt);

From the momentjs source, that ISO format should be accepted, assuming the comment doesn't lie and it Works Here (TM) against MomentJS 2.2.1.

// iso 8601 regex
// 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00)
isoRegex = /^\s*(?:[+-]\d{6}|\d{4})-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/,
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top