我需要使用javascript获取不同地点的当前时间。 我将使用以下方法获取 UTC。
function calcUTC() {
// create Date object for current location
var d = new Date();
// convert to msec
// subtract local time zone offset
// get UTC time in msec
var utc = d.getTime() - (d.getTimezoneOffset() * 60000);
return utc;
}
现在我需要找到特定地点的 timezoneOffset,以便我可以将此偏移量添加到 utc 来获取该位置的当前时间。 这些地方可以是美国、加拿大或任何其他地方。美国有三个不同的时区。请做可能的事 谢谢
最佳答案
Date对象本身的getTime()方法返回UTC值。
引用:MDN Date object getTime Method
它说,
Method returns the numeric value corresponding to the time for the specified date according to universal time.
您不需要减去或添加本地时区偏移。
为了计算其他时区的本地时间,您需要找到这些时区的偏移值(这应该考虑到夏令时)。
注意:JavaScript Date 对象不提供任何将时区作为输入并返回该时区偏移量的方法。
此外,如果偏移值是绝对值,您将需要减去或添加偏移量,具体取决于时区是在 GMT 之前还是之后。
关于javascript - 如何在javascript中获取不同地点的时区偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31444301/