3

Assuming the following UTC RFC 3339 timestamp:

2012-09-30 12:12:12Z

What is a good way of generating a localized Date in JavaScript?

I've arrived at the following convoluted (but working) solution, and I cannot help but thinking that I have missed something.

/**
 * Accepts a string on the format YYYY-MM-DD HH:MM:SS
 * and returns a localised Date
 */
DateUtils.localised_date_from = function (rfc_timestamp) {
    var date_parts = rfc_timestamp.substring(0, 10).split("-"),
        time_parts = rfc_timestamp.substring(10).split(":"),
        year = parseInt(date_parts[0], 10),
        month = parseInt(date_parts[1], 10) - 1,
        date = parseInt(date_parts[2], 10),
        hours = parseInt(time_parts[0], 10),
        minutes = parseInt(time_parts[1], 10),
        seconds = parseInt(time_parts[2], 10),
        utc_timestamp = Date.UTC(year, month, date, hours, minutes, seconds);

    return new Date(utc_timestamp);
};

Edit:

Date.parse ought to be a great starting point but simply does not work with RFC3339 in older browsers, at least not in IE8 where new Date(Date.parse("2012-09-30 12:12:12Z")) returns NaN.

Jon Nylander
  • 8,743
  • 5
  • 34
  • 45
  • 1
    I assume you've tried `Date.parse()` ? I'm not sure how it handles that particular format offhand – BLSully Oct 31 '12 at 18:45
  • I have not! Hah, works great in Chrome! Thanks, now... I will try in IE 7 et al. – Jon Nylander Oct 31 '12 at 18:51
  • 1
    possible duplicate of [How to convert date in RFC 3339 to the javascript date object(milliseconds since 1970)](http://stackoverflow.com/questions/11318634/how-to-convert-date-in-rfc-3339-to-the-javascript-date-objectmilliseconds-since) – Bergi Oct 31 '12 at 19:08

1 Answers1

4

(Post-comment)

new Date(Date.parse("2012-09-30 12:12:12Z")).

Gives me:

Sun Sep 30 2012 07:12:12 GMT-0500 (Central Daylight Time)

EDIT

I can't find (quick google) whether parsing RFC 3339 is part of ECMA script spec or not, so no guarantees this is cross-browser. Worked for me in Chrome 22.

Might be worth checking out http://www.datejs.com/ if you run into issues

BLSully
  • 5,929
  • 1
  • 27
  • 43
  • 2
    You can read about a Time-Format specified to be parsed in [EcmaScript § 15.9.1.15](http://es5.github.com/#x15.9.1.15). Yet, most browser should also have implemented ISO8601 of which RFC3339 is a subset of. However, I don't think that is guaranteed for all, especially older, browsers... – Bergi Oct 31 '12 at 18:59
  • Actually, I do remember trying Date.parse, it returns NaN in IE8. I should have specified in the answer that I wanted a cross browser solution. – Jon Nylander Oct 31 '12 at 19:13
  • @JonNylander: Check out `Date.js` I linked... there's a function in there `Date.parseExact` which I believe will give you what you need. However, if this is the only thing you need a date function for....maybe you don't want to load a whole library. It's incredibly useful though if you do any sort of regular work with Dates in JS – BLSully Oct 31 '12 at 19:21
  • I am familiar with Date.js and it is indeed handy. I do want to skip the overhead of that library since I feel that as long as dates are stored as Unix UTC timestamps, all browsers seem to localize well enough. This project also has its own JS date library injected into the codebase, and it is prototyping on the Date object like crazy. So not an option in this case, but in most cases it should be. I'll put a checkmark on your answer now. Thanks for spending the time. – Jon Nylander Oct 31 '12 at 19:25
  • Makes sense. Bergi's link had some good info in it too, though I can't argue for a good reason to include it (function in accepted answer) in place of what is in your question. – BLSully Oct 31 '12 at 19:28
  • Yeah. I'll keep "my" solution for now. Your answer is correct, it's just that the volatile nature of the browser environment makes it hard to use right of the bat. But that is really browser related, and not JS related. – Jon Nylander Nov 01 '12 at 05:59