Search code examples
javascripttimezonedatejs

Reading a date string with a different time zone in javascript


I have a datestring, pulled from an external source, that looks like this:

9/25/2011 4:38:40 PM

That source in the the PDT timezone.

I'd like to create a UTC date out of that information, using date.js. I'm using this code to parse it at present:

var dateString = '9/25/2011 4:38:40 PM';
var d = Date.parseExact('9/25/2011 4:38:40 PM', 'M/d/yyyy H:m:s tt');

While this does load the date, it does so as if it were in my timezone. How can I tell date.js that the date I'm telling it is from a different time zone?


Solution

  • Use timezone format specifier...

    var dateString = '9/25/2011 4:38:40 PM EST';
    var d = Date.parseExact(dateString, 'M/d/yyyy H:m:s tt Z');