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