Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

date - Javascript countdown using absolute timezone?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...