使用绝对时区的Javascript倒计时? [英] Javascript countdown using absolute timezone?

查看:249
本文介绍了使用绝对时区的Javascript倒计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript倒计时器,它通过获取指定的日期和时间,并将其与当前日期和时间进行比较。问题是,当前时间是相对于用户时区的,因此用户之间的剩余时间是不同的。



我怎样才能让计时器倒计时直到具体的时区,在我的情况下GMT -5小时?



我知道我可以使用下面的代码来获取用户的时区,但我迷失了如何使用它。


myDateObj.getTimezoneOffset()/ 60



将本地时间转换为另一个时区-with-this-javascript



在文章后面逐字逐句地告诉你这个例子:

  var d = new Date(); 

var localTime = d.getTime();

var localOffset = d.getTimezoneOffset()* 60000;

var utc = localTime + localOffset;

//获取并添加目的地的UTC时间偏移
//例如,Bombay
//这是UTC + 5.5小时
var offset = 5.5;
var bombay = utc +(3600000 * offset);

var nd = new Date(bombay);
alert(Bombay time is+ nd.toLocaleString()+< br>);

jsFiddle: http://jsfiddle.net/GEpaH/



只需使用您想要的偏移进行更新,您应该全部设置好。


I have a javascript countdown timer that works by taking a specified date and time, and comparing it to the current date and time. The issue is, the current time is relative to the users timezone, so the time remaining is different between users.

How can I have the timer countdown till a time in a specific timezone, in my case GMT -5 hours?

I understand i can use the below code to get the users timezone, but I am lost as how to use this.

myDateObj.getTimezoneOffset( ) / 60

解决方案

A quick search reveals: convert-the-local-time-to-another-time-zone-with-this-javascript

Following the article verbatim gets you this example:

var d = new Date();

var localTime = d.getTime();

var localOffset = d.getTimezoneOffset() * 60000;

var utc = localTime + localOffset;

// obtain and add destination's UTC time offset
// for example, Bombay 
// which is UTC + 5.5 hours
var offset = 5.5;   
var bombay = utc + (3600000*offset);

var nd = new Date(bombay); 
alert("Bombay time is " + nd.toLocaleString() + "<br>");

jsFiddle: http://jsfiddle.net/GEpaH/

Just update with your desired offset and you should be all set.

这篇关于使用绝对时区的Javascript倒计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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