如何在javascript中使用服务器的系统时区和UTC时间获取当前本地时间

时间:2012-05-19 11:51:13

标签: javascript jquery datetime utc

我在json响应中的UTC时间为Sat, 19 May 2012 11:26:51 +0000,系统时区为+0530。如何使用两个可用结果将UTC时间转换为本地时间?我希望日期为isoDate格式,时间为isoTime格式。请帮忙怎么做?

1 个答案:

答案 0 :(得分:1)

将JSON日期时间转换为日期对象(使用不带+0000时区信息的字符串)并添加/减去客户端timezoneOffset:

var received = new Date('Sat, 19 May 2012 11:26:51'),
    clientDate = new Date(new Date().getTimezoneOffset()*-60000 
                  + received.getTime());
//note: -60000 reverses the sign of the timezone offset 
//      clientDate is calculated in milliseconds.
//alternatively you can set [received] directly to the local datetime using:
received.setMinutes(received.getMinutes()+(-(new Date().getTimezoneOffset()));

在我的时区(GMT + 2)clientDate现在读取Sat May 19 2012 13:26:51 GMT+0200