2

How to set an EST time in a countdown using Javascript. I am using the following piece of code and able to set the hours countdown , but i need a particular time as
http://www.timeanddate.com/countdown/generic?iso=20130920T1730&p0=152/

var now = new Date();
BigDay = new Date("September20,2013");
var countTo=BigDay.getTime()

 $('.timer').countdown(countTo, function(event) {
    var $this = $(this);
    switch(event.type) {
        case "seconds":
        case "minutes":
        case "hours":
        case "days":
        case "weeks":
        case "daysLeft":
            $this.find('span.'+event.type).html(event.value);
            break;
        case "finished":
            $this.hide();
            break;
    }
});
1
  • You should really provide us poor code monkeys with some supplementary code, like your HTML at least so we can see what you're up to! :') Commented Sep 17, 2013 at 19:18

1 Answer 1

5

Is this along the lines of what you're looking for? First off, some HTML:

<span id="days"></span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>

Then, some Javascript (together with jQuery!):

setInterval(function(){
    // set whatever future date / time you want here, together with
    // your timezone setting...
    var future = new Date("Sep 20 2014 21:15:00 GMT+0200");
    var now = new Date();
    var difference = Math.floor((future - now) / 1000);

    var seconds = fixIntegers(difference % 60);
    difference = Math.floor(difference / 60);

    var minutes = fixIntegers(difference % 60);
    difference = Math.floor(difference / 60);

    var hours = fixIntegers(difference % 24);
    difference = Math.floor(difference / 24);

    var days = difference;

    $("#seconds").text(seconds + "s");
    $("#minutes").text(minutes + "m");
    $("#hours").text(hours + "h");
    $("#days").text(days + "d");
}, 1000);

function fixIntegers(integer)
{
    if (integer < 0)
        integer = 0;
    if (integer < 10)
        return "0" + integer;
    return "" + integer;
}

I hope this makes sense! Here's a fiddle if you'd like to see the live version in action.

You can also see this Countdown Timer using jQuery

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. How can i make to work on firefox as well , this works fine on chrome.
Hey @user2619093, sorry bout that, I fixed the future variable initialisation, and it's working in Firefox now... ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.