Javascript - 从 rawOffset Google 时区计算时间 API
Javascript - Calculate time from the rawOffset Google timezone API
我正在开发一个应用程序来获取城市的当地时间。我正在使用 google 地理编码 API 来获取城市的纬度和经度,并将其传递给时区 API。
时区APIreturn以下
{
"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);
但是 London, Washington, NewYork
等城市的时间延迟一小时,return 是 Arizona
的正确时间。
还有。我不知道 local time
和 rawOffset
参数之间的这种关系来计算时间。请解释这个 rawOffset
参数是什么以及如何解决某些城市的一小时延迟问题。
在 Google TimeZone 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
传递。当我用以下代码替换 timestamp 时,我从 Google APIs 得到了 dstOffset 和 rawOffset 的正确响应。
myDate = new Date('1/1/1970');
timeStamp = myDate.getTime();
任何人都可以给我它是如何工作的技术解释吗?
您没有考虑夏令时偏移,这就是为什么某些城市会延迟一个小时的原因。
calcTime(rawOffset+dstOffset);
时间戳包含日期和时间,因为 DST 只发生在一年中的特定时期。
我正在开发一个应用程序来获取城市的当地时间。我正在使用 google 地理编码 API 来获取城市的纬度和经度,并将其传递给时区 API。
时区APIreturn以下
{
"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);
但是 London, Washington, NewYork
等城市的时间延迟一小时,return 是 Arizona
的正确时间。
还有。我不知道 local time
和 rawOffset
参数之间的这种关系来计算时间。请解释这个 rawOffset
参数是什么以及如何解决某些城市的一小时延迟问题。
在 Google TimeZone 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
传递。当我用以下代码替换 timestamp 时,我从 Google APIs 得到了 dstOffset 和 rawOffset 的正确响应。
myDate = new Date('1/1/1970');
timeStamp = myDate.getTime();
任何人都可以给我它是如何工作的技术解释吗?
您没有考虑夏令时偏移,这就是为什么某些城市会延迟一个小时的原因。
calcTime(rawOffset+dstOffset);
时间戳包含日期和时间,因为 DST 只发生在一年中的特定时期。