如何在另一个时区获取本地时间
How to get local time in another time zone
使用纯 javascript(没有 jquery 也没有插件),如何将一个本地时间转换为另一个时区?
情况:
我正在做一个倒数计时器,我已将结束日期设置为 javascript 变量。我想根据客户端 location/time 区域将该变量转换为本地时间。
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。
有什么想法吗?
var now = new Date();
这将为您提供客户日期和时间。
now.getTimezoneOffset()
将为您提供该时区与 UTC 之间的时差。
您可以在 UTC 中指定您的维护结束时间,让本地浏览器为您处理时间转换:
var maintenanceEndDate = new Date("2015-11-10T00:00:00Z");
document.write('Local time: ' + maintenanceEndDate.toString() + '<br>')
document.write('UTC time: ' + maintenanceEndDate.toISOString())
我建议您深入研究 momentjs 库,它可以满足您的需求:http://momentjs.com/ and http://momentjs.com/timezone/
使用纯 javascript(没有 jquery 也没有插件),如何将一个本地时间转换为另一个时区?
情况:
我正在做一个倒数计时器,我已将结束日期设置为 javascript 变量。我想根据客户端 location/time 区域将该变量转换为本地时间。
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。
有什么想法吗?
var now = new Date();
这将为您提供客户日期和时间。
now.getTimezoneOffset()
将为您提供该时区与 UTC 之间的时差。
您可以在 UTC 中指定您的维护结束时间,让本地浏览器为您处理时间转换:
var maintenanceEndDate = new Date("2015-11-10T00:00:00Z");
document.write('Local time: ' + maintenanceEndDate.toString() + '<br>')
document.write('UTC time: ' + maintenanceEndDate.toISOString())
我建议您深入研究 momentjs 库,它可以满足您的需求:http://momentjs.com/ and http://momentjs.com/timezone/