1

我得到了Sat, 19 May 2012 11:26:51 +0000json 响应中的 UTC 时间,并且系统时区为+0530. 如何使用两个可用结果将 UTC 时间转换为本地时间?我希望日期为isoDate格式,时间为isoTime格式。请帮助如何做到这一点?

4

1 回答 1

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

于 2012-05-19T12:08:01.417 回答