Search code examples
javascriptdatetimecountdown

How do I set a countdown's target time to be in a set time zone?


I'm working on a countdown webpage for a huge update on a game that will be a big thing and whatnot. That countdown is working currently with this code getting the times of the computer, the target end time and the time difference:

var christmas = new Date("December 25, 2014 00:01:00");
var now = new Date();
var timeDiff = christmas.getTime() - now.getTime();

I need a way to get the christmas target time to be in the same timezone no matter what your computer's timezone is set to.


Solution

  • Use UTC:

    var xmas = new Date('2014-12-25T00:00:01.0000000'); 
    var utc = new Date(
                  xmas.getUTCFullYear(),
                  xmas.getUTCMonth(),
                  xmas.getUTCDate(),
                  xmas.getUTCHours(),
                  xmas.getUTCMinutes(),
                  xmas.getUTCSeconds()
              );
    
    alert('local diff: ' + (xmas.getTime() - new Date().getTime()));
    alert('utc diff: ' + (utc.getTime() - new Date().getTime()));