Virat Kohli
Virat Kohli

Reputation: 221

How to have a countdown timer to a date and time

I have this code:

<script>
    $(function () {
        var march04 = new Date();
        var march07 = new Date();
        var march11 = new Date();
        var march13 = new Date();
        var august09 = new Date();

        // march04 = new Date(march04.getFullYear() + 0, 3 - 1, 10);
        march07 = new Date(march07.getFullYear() + 0, 3 - 1, 8);
        march11 = new Date(march11.getFullYear() + 0, 3 - 1, 12);
        march13 = new Date(march13.getFullYear() + 0, 3 - 1, 14);
        august09 = new Date(august09.getFullYear() + 0, 8 - 1, 10);

        $('#march04').countdown({until: march04});
        $('#march07').countdown({until: march07});
        $('#march11').countdown({until: march11});
        $('#march13').countdown({until: march13});
        $('#august09').countdown({until: august09});

    });
</script>

At the moment what it is doing is counting till the date but I also want it to countdown to a specific time for example august09 19:00pm. Could you please help me out? Thanks.

Upvotes: 1

Views: 290

Answers (2)

Laxmikant Dange
Laxmikant Dange

Reputation: 7698

I think you might need this

var today = new Date();
var target = new Date("05-28-2015");
var diff = new Date(target - today);
var str = diff.getMonth() + ":" + diff.getDate() + " " + diff.getHours() + ":" + diff.getMinutes() + ":" + diff.getSeconds() + ":" + diff.getMilliseconds();

Here is a sample.

Upvotes: 1

DmitryK
DmitryK

Reputation: 5582

The way you specify date is just a full number of days (i.e. you don't specify time).

You can do it directly

var march07_1900 = "March 7, 2015 19:00:00"; 

Or using your style: If you don't specify anything then Date() will return current date/time.

Full set of parameters looks like this:

new Date(year, month, day, hours, minutes, seconds, milliseconds) 

So using your style:

var march07_1900 = new Date();
march07_1900 = new Date(march07_1900.getFullYear() + 0, 3 - 1, 7, 19, 0, 0);
document.write(march07_1900);

and then you do

$('#march07').countdown({date: march07_1900});

Upvotes: 1

Related Questions