谷歌时区
Google Timezone
我对使用谷歌时区有点困惑,虽然这工作得很好,但我无法获得正确的时间。
这是我的代码:
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var timeStamp = Math.floor(new Date().getTime() / 1000);
var url = "https://maps.googleapis.com/maps/api/timezone/json?location=41.010734,9.0039×tamp=" + timeStamp ;
xhr.open("GET", url, false);
xhr.send(null);
var resp = JSON.parse(xhr.responseText);
var time = new Date((resp.dstOffset + resp.rawOffset + timeStamp) * 1000);
var span = " AM";
var hours = time.getUTCHours();
if (hours > 12) {
span = " PM";
hours -= 12;
}
CurrentTime = ((hours + '').length == 1 ? '0' + hours : hours) + ":" + ((time.getUTCMinutes() + '').length == 1 ? '0' + time.getUTCMinutes() : time.getUTCMinutes()) + span;
console.log("Time is: " + CurrentTime);
我正在使用并返回数据,然而,因为我正在从英国传递时间并检索法国的一个位置,所以时间减少了1小时。我知道这与DST和返回数据的原始值有关,但无法确定如何确保这始终是正确的。
要获取utc时间戳,可以使用:
var now = new Date;
var utc_timestamp = Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() ,
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds(),
now.getUTCMilliseconds()
);
更新小提琴:
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var now = new Date;
var utc_timestamp = Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() ,
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds(),
now.getUTCMilliseconds()
);
var timeStamp = Math.floor(utc_timestamp / 1000);
var url = "https://maps.googleapis.com/maps/api/timezone/json?location=41.010734,9.0039×tamp=" + timeStamp ;
xhr.open("GET", url, false);
xhr.send(null);
var resp = JSON.parse(xhr.responseText);
var time = new Date((resp.dstOffset + resp.rawOffset + timeStamp) * 1000);
var span = " AM";
var hours = time.getUTCHours();
if (hours > 12) {
span = " PM";
hours -= 12;
}
CurrentTime = ((hours + '').length == 1 ? '0' + hours : hours) + ":" + ((time.getUTCMinutes() + '').length == 1 ? '0' + time.getUTCMinutes() : time.getUTCMinutes()) + span;
console.log("Time is: " + CurrentTime);
相关文章:
- 谷歌地图固定位置覆盖
- 不显示带有本地json文件数据的谷歌地图脚本
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- 谷歌地图标记不会显示
- 无法在JS中显示谷歌地图
- 科尔多瓦页面类应用程序中的多个谷歌地图
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 在谷歌地图上获取事件的x,y坐标
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 模拟谷歌地图中的点击
- 谷歌文档表面引擎
- 谷歌时区API密钥
- 谷歌关闭时区
- 谷歌地图时区获取当地时间
- 支持用户时区在嵌入式谷歌日历
- 谷歌时区
- 哪一种是自定义的,通过书本的方式获取用户的本地时间/时区?(或者:谷歌/脸书是怎么做到的?)