1

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?

2
  • Please note that there is no standard for timezone abbreviations or acronyms and many are ambiguous (EST is used for 3 different timezones). It is far better to use the actual offset (per ECMA-262 15.9.1.15). Commented Sep 27, 2011 at 21:20
  • @RobG: I completely forgot about that - good point. Commented Sep 27, 2011 at 21:31

3 Answers 3

1

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');
Sign up to request clarification or add additional context in comments.

3 Comments

I don't actually have control of the string. Should I just append the time zone on the end before parsing it?
@Eric, which string? The dateString or the format string? If dateString then you could append the timezone, assuming you know the value in all the cases.
dateString. Although as @RobG says, I should use an offset rather than a 3 letter code.
1

putting an e in a date format will signify the timezone. I haven't tested this, but:

Date.parseExact(dateString + " PDT", "M/d/yyyy H:m:s tt e")

Does not account for daylight savings time shifts (PST instead of PDT), but you get the gist.

Comments

0

Have you tried something like this?:

var dateString = '9/25/2011 4:38:40 PM';

var date = Date.parseExact(dateString, format);
var utc_date = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.