• API
• FAQ
• Tools
• Archive
daily pastebin goal
38%
SHARE
TWEET

Javascript Date Countdown

sweenig Jan 9th, 2014 1,254 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function countdown(yr,mon,d,h,m,pastmessage,target){
2.  //yr,mon,d,h,m are the Year, Month, Day, Hour, and Minute of the date the future event occurs
3.  //pastmessage is the message to display when the event has already happened
4.  //target is the target element to write the output to
5.  var today=new Date();
6.  var futuredate=new Date(yr,mon-1,d,h,m); //don't care why but the month has to be decremented
7.  dd=futuredate-today; //this should be the difference in the two dates in milliseconds
8.  dday=Math.floor(dd/(60*60*1000*24)*1); //find the number of days in dd
9.  dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1); //find the number of hours in dd
10.  dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1); //find the number of minutes in dd
11.  var wholedmin=dmin;if (dmin < 10){wholedmin="0"+dmin;} //convert to 2 digits if necessary
12.  dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1); //find the number of seconds in dd
13.  var wholedsec=dsec;if (dsec < 10){wholedsec="0"+dsec;} //convert to 2 digits if necessary
14.  if(futuredate<=today){
15.   document.getElementById(target).innerHTML=pastmessage; //write this if the date is past
16.  }
17.  else {
18.   document.getElementById(target).innerHTML= dday+ " days, "+dhour+":"+wholedmin+":"+wholedsec; //write this is futuredate is in the future
19. }}
20. </script>
22. <!--this div is the container for the element that gets updated every second.  Reformat as needed.-->
23. <h1 id="starwars">
24. <!--this element's inner content is re-written by the countdown function --></h1>
25. </div>
26. <br />