谷歌地图时区获取本地时间 [英] google map Time Zone get local time
本文介绍了谷歌地图时区获取本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用谷歌时区API。我公司提供的经度和纬度和API给我的时区。
I'm trying to use google Time Zone API. I provide the longitude and latitude and the API give me the timezone.
我怎样才能获得本地时间与此下面的值(字符dstOffset和rawOffset)?
How can I get the local time with this following value (dstOffset and rawOffset) ?
下面是JSON的
{
"dstOffset" : 0.0,
"rawOffset" : -28800.0,
"status" : "OK",
"timeZoneId" : "America/Los_Angeles",
"timeZoneName" : "Pacific Standard Time"
}
我已经试过这个javascript函数,但我没有得到正确的时间。
I have tried this javascript function but I don't get the correct time.
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);
感谢您的帮助!
推荐答案
您正在接近。问题是在这一行:
You're close. The problem is in this line:
var nd = new Date(utc + (3600000*offset));
偏移量由JSON的给予似乎是秒;你对待它,就好像它是在小时。它更改为以下,它应该开始工作:
The offset given by the Json appears to be in seconds; you are treating it as though it is in hours. Change it to the following and it should start working:
var nd = new Date(utc + (1000*offset));
下面是工作的jsfiddle 。
这篇关于谷歌地图时区获取本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文