Subtract date to get countdown

Asked

Viewed 2,257 times

2

I currently have the following code:

var now = new Date();
var countTo = 50 * 24 * 60 * 60 * 1000 + now.valueOf();
$('.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;
    }
});

Every time I reload the page the count starts again. I would need to set a specific date (14:30:00 08/01/2015) for comparison and countdown for that day.

The count currently starts from 50 days.

  • What plugin are you using? can you make a jsFiddle with an example?

1 answer

2


Your code is using the current date (+50 days) for the countdown. If you change the value of this variable to a fixed number (fixed date) then you will have the behavior you want.

For example:

var countTo = new Date('14:30:00 08/01/2015');

I don’t know exactly which plugin you use, but supposing it is the .Countdown() jQuery can see an example here:

http://jsfiddle.net/Tc5tn/

Note: as per the @abfurlan mentioned in the comments below, it would be better to use the format "8 January 2015 14:30:00:00" because my first suggestion may be misread if the computer’s Locale is different.

  • 1

    The date format would not be "8 January 2015 14:30:00:00"? http://jsfiddle.net/Tc5tn/1/, in your example this giving 410 days, I think it is wrong

  • 1

    @abfurlan in my case gives 204 days, but it is a good observation and its format is better than mine. Here on the European side my format works "by chance"

  • Interesting, perhaps it is interesting to describe in the answer

  • Thank you. I consoled the response to your reply Sergio with the consideration made by @abfurlan and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.