google map Time Zone get local time

∥☆過路亽.° 提交于 2019-12-07 12:02:48

问题


I'm trying to use google Time Zone API. I provide the longitude and latitude and the API give me the timezone.

How can I get the local time with this following value (dstOffset and rawOffset) ?

Here is the Json

{
   "dstOffset" : 0.0,
   "rawOffset" : -28800.0,
   "status" : "OK",
   "timeZoneId" : "America/Los_Angeles",
   "timeZoneName" : "Pacific Standard Time"
}

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);

Thanks for your help !


回答1:


You're close. The problem is in this line:

var nd = new Date(utc + (3600000*offset));

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));

Here is the working jsFiddle.



来源:https://stackoverflow.com/questions/15776424/google-map-time-zone-get-local-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!