如何获取其他时区的本地时间

How to get local time in another time zone

本文关键字:时区 时间 其他 何获取 获取      更新时间:2023-09-26

使用纯javascript(没有jquery或插件),如何将一个本地时间转换为另一个时区?

对于情况:

我正在做一个倒计时计时器,并将结束日期设置为javascript变量。我想根据客户端的位置/时区将该变量转换为本地时间。

var maintenanceEndDate = new Date("11/10/2015 00:00"); //This is the initial time,  based on the administrator's location
var maintenanceEndDateOffset = ????  //The above time converted to local time

然后我想把两者都转换成UTC。

有什么想法吗?

您可以用UTC指定维护结束时间,并让本地浏览器为您处理时间转换:

var maintenanceEndDate = new Date("2015-11-10T00:00:00Z");
document.write('Local time: ' + maintenanceEndDate.toString() + '<br>')
document.write('UTC time: ' + maintenanceEndDate.toISOString())

var now = new Date();

这会给你客户的日期和时间。

now.getTimezoneOffset()

将为您提供该时区和UTC之间的偏移量。

我建议您深入momentjs库,它正是您所需要的:http://momentjs.com/和http://momentjs.com/timezone/