0

I retrieved a datetime value from a mysql datebase (a 'datetime' column value), and after retrieving it via an ajax call, when I created the Date object, it considered the datetime to be in EST time. But in the DB its stored in GMT time. How can I get javascript to consider the value as GMT time?

Here's my code:

timestamp = new Date(data[i].startTime);
alert(timestamp.getTime());

The alert shows the same exact time, but considers it in local time (EST). IE: If data[i].startTime reads as "2012-11-27 09:05:18", then the alert reports 1354025118. This number of milliseconds though refers to 2012-11-27 09:05:18 GMT-0500 (or 2012-11-27 14:05:18)

4

1 回答 1

0

如何让 javascript 将该值视为 GMT 时间?

要使Date构造函数不使用本地(当前用户)时区,请使用带有时区信息的日期时间字符串,例如"2012-11-27 09:05:18 GMT". 然而,很难找到一种可以被每个浏览器可靠解析的格式——Date.parse算法是依赖于实现的。更好的是,使用 unix 时间戳,即自unix epoch以来的毫秒数,或者使用正则表达式将字符串分解为各个部分,然后将它们输入Date.UTC.

于 2013-01-31T16:52:39.970 回答