Jump to content

Javascript Countdown Timer Between Two Dates


dlebowski

Recommended Posts

I want to be able to take two dates with times and want to show a realtime countdown. I need it to be to the second. For example:

 

Start:

Year: 2012

Day: 09

Month: 12

Hour: 10

Minute: 12

Second: 35

 

End:

Year: 2012

Day: 09

Month: 12

Hour: 10

Minute: 13

Second: 45

 

Displayed: 0 days 0 hours 1 minute 10 seconds

 

I want this countdown realtime on the screen. So far, all of the scripts I have found have been to the minute and also used the client sides clock to determine the countdown. I just need it to countdown in seconds and utilize to specified dates and times.

 

Any help with this would be awesome. Thanks in advance.

Okay. I'm getting closer. Here is my code:

 

var date1, date2, sec, hours, min;
    window.onload = function () {
	    //MM/DD/YYYY HH:MM:SS PM or AM
	    date1 = new Date("04/12/2011 07:14:00 PM");
	    date2 = new Date("04/12/2011 07:14:20 PM");
	    sec = (date2.getTime() / 1000.0) - (date1.getTime() / 1000.0);
	    hours = parseInt(sec / 60 / 60);
	    sec = sec - hours * 60 * 60;
	    min = parseInt(sec / 60);
	    ses = sec - min * 60;
	    calc();
    }
    function calc() {
	    if (hours<=0&&min<=0&&sec<=0)
	    {
		    alert("cool");
	    }

	    else
	    {
		    sec--;
	    var day=Math.floor(hours/(60*60*1000*24)*1)

	    day.innerHTML = day;
	    hrs.innerHTML = hours;
	    mins.innerHTML = min;
	    secs.innerHTML = sec;
	    setTimeout("calc()", 1000);
	    }
    }

 

All I need to do now, is just have the hours become days if they are more than 24. For example if hours are 25, this is what it would look like:

 

1 days 1 hours 1 minute 10 seconds

 

Current it does this:

 

25 hours 1 minutes 10 seconds

 

Again, any help would be great. Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.