13

Google calendar throws at me rfc3339, but all my dates are in those milliseconds since jan 1970.

rfc3999:

2012-07-04T18:10:00.000+09:00

javascript current time: (new Date()).getTime():

1341346502585

I prefer the the milliseconds because I only deal in countdowns and not in dates.

DoTheEvo
  • 874
  • 1
  • 9
  • 21
  • 1
    By the way [RFC 3339](https://tools.ietf.org/html/rfc3339) is a profile of the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard. See [What's the difference between ISO 8601 and RFC 3339 Date Formats?](https://stackoverflow.com/q/522251/642706). – Basil Bourque Feb 06 '18 at 20:13

2 Answers2

22

Datetimes in that format, with 3 decimal places and a “T”, have well-defined behaviour when passed to Date.parse or the Date constructor:

console.log(Date.parse('2012-07-04T18:10:00.000+09:00'));
// 1341393000000 on all conforming engines

You have to be careful to always provide inputs that conform to the JavaScript specification, though, or you might unknowingly be falling back on implementation-defined parsing, which, being implementation-defined, isn’t reliable across browsers and environments. For those other formats, there are options like manual parsing with regular expressions:

var googleDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})([+-]\d{2}):(\d{2})$/;

function parseGoogleDate(d) {
    var m = googleDate.exec(d);
    var year   = +m[1];
    var month  = +m[2];
    var day    = +m[3];
    var hour   = +m[4];
    var minute = +m[5];
    var second = +m[6];
    var msec   = +m[7];
    var tzHour = +m[8];
    var tzMin  = +m[9];
    var tzOffset = tzHour * 60 + tzMin;

    return Date.UTC(year, month - 1, day, hour, minute - tzOffset, second, msec);
}

console.log(parseGoogleDate('2012-07-04T18:10:00.000+09:00'));

or full-featured libraries like Moment.js.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 2
    Thank you, works like a charm. I used huge code with regex and it stopped working for me, then I spend lots of time googling and reading here but nothing, until now. I am using it in browser extension so it doesn't need to be crossplatform. thanks – DoTheEvo Jul 03 '12 at 20:51
  • Slightly modified version [here](https://jsfiddle.net/qm9osm4a/) which copes with Z or missing timezone, missing : in timezone, and missing milliseconds. – Crashthatch Apr 19 '16 at 12:11
  • What about Node.js? Is it reliable on Node? – rustyx Nov 20 '19 at 14:14
  • @rustyx: In practice, yes, but I still wouldn’t use it for formats outside of the ES spec. There are date libraries for that. – Ry- Nov 20 '19 at 18:43
  • 2012: `Date.parse() is notoriously unreliable across browsers` - it's now 2020. Does this statement still hold true? – colm.anseo Mar 27 '20 at 18:33
  • @colminator: Yes, you should still only use the formats guaranteed to work by the JavaScript specification. https://tc39.es/ecma262/#sec-date-time-string-format (The one in the question is actually a spec one.) – Ry- Mar 27 '20 at 23:34
  • @Ry so as long as my REST service returns RFC3339, will Date.parse() work across browsers? I would think/hope so! – colm.anseo Mar 28 '20 at 00:21
  • @colminator: It has to be a little stricter than RFC 3339 (RFC 3339 allows `T` to be replaced with space, `Date` doesn’t; RFC 3339 allows any number of decimal digits for fractional seconds, `Date` requires exactly 3, etc. – read both specs if you want to test the boundaries of this), but yes. Return `YYYY-MM-DDTHH:mm:ss.sssZ` and you’ll be fine. – Ry- Mar 28 '20 at 00:26
4

There are two Javascript date libraries that you could try:

Both of these will give you functions that allow you to parse and generate dates in pretty much any format.

If you're working with dates a lot, you'll want to use use one of these libraries; it's a whole lot less hassle than rolling your own functions every time.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307