【问题标题】:How to get the current local time using the system time zone and UTC time from the server in javascript如何在javascript中使用系统时区和UTC时间从服务器获取当前本地时间
【发布时间】:2012-05-19 11:51:12
【问题描述】:
我在 json 响应中将 UTC 时间设置为 Sat, 19 May 2012 11:26:51 +0000,并将系统时区设置为 +0530。如何使用两个可用结果将 UTC 时间转换为本地时间?我希望日期为isoDate 格式,时间为isoTime 格式。请帮助如何做到这一点?
【问题讨论】:
标签:
javascript
jquery
datetime
utc
【解决方案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