开发者

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 sour开发者_如何学Goce 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?


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');


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.


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)) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜