0

I found this piece of Javascript on StackOverFlow which displays a live countdown to a specific date.

<script>
var end = new Date('02/19/2012 10:1 AM');

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'EXPIRED!';

            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = days + ' Days ';
        document.getElementById('countdown').innerHTML += hours + ' Hours ';
        document.getElementById('countdown').innerHTML += minutes + ' Minutes ';
        document.getElementById('countdown').innerHTML += seconds + ' Seconds';
    }

    timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>

However, I want to make it so that the time it uses is EST. So that if people view the counter from different timezones, they will see the same thing. How can I accomplish this? I'm new to JavaScript and I don't know where to go from here.

user2680030
  • 53
  • 1
  • 6
  • `new Date('02/19/2012 10:1 AM UTC');` or something... just make sure to change the date to UTC time – jeremy Aug 17 '13 at 19:50

1 Answers1

0

This would work:

<script>
var end = new Date('02/19/2012 10:1 AM EST');

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function getESTOffset() {

    return new Date().getTimezoneOffset() - (end.getTimezoneOffset())
}

function showRemaining() {
    var now = new Date();
    var distance = end - now - getESTOffset() * _hour;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById('countdown').innerHTML = 'EXPIRED!';

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('countdown').innerHTML = days + ' Days ';
    document.getElementById('countdown').innerHTML += hours + ' Hours ';
    document.getElementById('countdown').innerHTML += minutes + ' Minutes ';
    document.getElementById('countdown').innerHTML += seconds + ' Seconds';
}

timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>

The key thing here is setting the end in the timezone you want it to be in (EST) and using it's offset before the calculations.

aa333
  • 2,556
  • 16
  • 23
  • Some browsers might allow `"EST"` in the input string, but that's nonstandard and not guaranteed. Even then, it would just adjust the internal value at the time of parsing. By the time you call `getTimezoneOffset`, you're still using the *local* time zone for the computer that the code is running on - not EST. – Matt Johnson-Pint Jun 06 '14 at 17:59