gpt4 book ai didi

Javascript - 从 rawOffset Google 时区 API 计算时间

转载 作者:行者123 更新时间:2023-12-02 16:11:02 24 4
gpt4 key购买 nike

我正在开发一个应用程序来获取城市的本地时间。我正在使用 google Geocode API 来获取城市的纬度和经度,并将其传递给时区 API。

时区 API 返回以下内容

{
"dstOffset" : 0,
"rawOffset" : 0,
"status" : "OK",
"timeZoneId" : "Europe/London",
"timeZoneName" : "Greenwich Mean Time"
}

我需要计算以下的时间。从这个SO answer ,我得到了以下代码来从 rawOffset 获取本地时间。

function calcTime(offset) {
var d = new Date();
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = new Date(utc + (3600000*offset));

alert("The local time is " + nd.toLocaleString());
}
calcTime(-28800.0);

但是,对于伦敦、华盛顿、纽约等城市来说,这会导致时间延迟一小时,而对于亚利桑那州,则会返回正确的时间。

还有。我不知道本地时间rawOffset参数之间的关系来计算时间。请解释一下这个 rawOffset 参数是什么,以及如何修复某些城市的一小时延迟。

在 Google 时区 API 中 Documentation ,我读到了

The local time of a given location is the sum of the timestamp parameter, and the dstOffset and rawOffset fields from the result.

这里的时间戳参数是什么?除了 1331161200 之外,我无法提供任何其他信息。

非常感谢任何帮助。提前致谢。

更新

FIX:这是因为我在时区 API 中将 timestamp 参数作为 1331161200 传递。当我用以下代码替换时间戳时,我从 Google API 获得了带有 dstOffset 和 rawOffset 的正确响应。

myDate = new Date('1/1/1970');
timeStamp = myDate.getTime();

谁能给我它如何工作的技术解释?

最佳答案

您没有考虑夏令时偏移,这就是为什么某些城市会晚一个小时。

calcTime(rawOffset+dstOffset);

时间戳包含日期和时间,因为夏令时仅发生在一年中的某些时段。 https://stackoverflow.com/a/10428184/3583980

关于Javascript - 从 rawOffset Google 时区 API 计算时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30210227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com