谷歌地图时区获取当地时间 [英] google map Time Zone get local time

查看:111
本文介绍了谷歌地图时区获取当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用谷歌时区 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆