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


You can use Date.UTC(year,month,day,hours,minutes,seconds,msec)

It operates just like the Date constructor, but returns the timestamp of the arguments at Greenwich time (offset=0) instead of local time.

var localtime=new Date(Date.UTC(year,month,day,hours,minutes,seconds,msec)) 

returns the local time for the UTC time specified.

Everyone (whose clock is set correctly) will end the countdown together.


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.


Just create a Date with an RFC 2822-timestamp with timezone. That time will then be converted to the users current location (based on OS settings). Even with corrections for daylight savings time!

I'm in Norway, which currently is in daylight savings time, so it's GMT+2. Here is what happens when I create a Date object using GMT-0500:

var myDateObj = new Date("Fri Apr 17 2015 12:00:00 GMT-0500 (CDT)");
myDateObj.toString();

Fri Apr 17 2015 19:00:00 GMT+0200 (CEST)

How to get the correct date string for your location? If it's the timezone you're currently in; just do myDateObj.toString() in your browsers dev-tools console. For a different timezone; change the timezone in your operating system first. (Or read the RFC)

new Date().toString();

Fri Apr 17 2015 12:36:57 GMT+0200 (CEST)