将时间戳转换为UTC(或GMT)时间?

时间:2021-12-30 21:49:04

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?

我从mysql数据库('datetime'列值)中检索了一个datetime值,在通过ajax调用检索它之后,当我创建Date对象时,它认为datetime是在EST时间。但在DB中它存储在GMT时间内。如何让javascript将值视为GMT时间?

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)

警报显示相同的确切时间,但在当地时间(EST)考虑。 IE:如果data [i] .startTime读作“2012-11-27 09:05:18”,则警报报告1354025118.这个毫秒数虽然是指2012-11-27 09:05:18 GMT-0500 (或2012-11-27 14:05:18)

1 个解决方案

#1


0  

How can I get javascript to consider the value as GMT time?

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

To make the Date constructor not use the local (current user) timezone, use a datetime string with timezone information, e.g. "2012-11-27 09:05:18 GMT". However, it is hard to find a format that is reliable parsed by every browser - the Date.parse algorithms are implementation-dependent. Better, use a unix timestamp, i.e. milliseconds since unix epoch, or use a regulare expression to break the string down in its parts and then feed those into Date.UTC.

要使Date构造函数不使用本地(当前用户)时区,请使用带有时区信息的日期时间字符串,例如“格林威治标准时间2012-11-27 09:05:18”。但是,很难找到每个浏览器可靠解析的格式 - Date.parse算法依赖于实现。更好的是,使用unix时间戳,即自unix纪元以来的毫秒,或者使用正则表达式将字符串分解为其部分,然后将其提供给Date.UTC。

#1


0  

How can I get javascript to consider the value as GMT time?

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

To make the Date constructor not use the local (current user) timezone, use a datetime string with timezone information, e.g. "2012-11-27 09:05:18 GMT". However, it is hard to find a format that is reliable parsed by every browser - the Date.parse algorithms are implementation-dependent. Better, use a unix timestamp, i.e. milliseconds since unix epoch, or use a regulare expression to break the string down in its parts and then feed those into Date.UTC.

要使Date构造函数不使用本地(当前用户)时区,请使用带有时区信息的日期时间字符串,例如“格林威治标准时间2012-11-27 09:05:18”。但是,很难找到每个浏览器可靠解析的格式 - Date.parse算法依赖于实现。更好的是,使用unix时间戳,即自unix纪元以来的毫秒,或者使用正则表达式将字符串分解为其部分,然后将其提供给Date.UTC。