Runtuk
Runtuk

Reputation: 31

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.

Upvotes: 0

Views: 330

Answers (2)

Madbreaks
Madbreaks

Reputation: 19539

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()));

Upvotes: 2

sam stone
sam stone

Reputation: 703

First Of All (just kidding) X-Mas Time Is "December 25, 2014 00:00:01" and if your page is served dynamically (like PHP), place server side date in html source like this:

<script>
var christmas = new Date("December 25, 2014 00:00:01");
var currentServerDate= <?= time() ?>
var dateDiff = christmas.getTime() - serverDate.getTime();
</script>

And No Matter What TimeZone Your Visitors Came From Its Always Will Be The Same

Upvotes: 0

Related Questions