First, check all the complete code:
View sourceprint? 01 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02
03
04
05 <meta content = "text/html; charset = UTF-8" http-equiv = "Content-Type"/>
06 <title> countDown test </title>
07
08 <script type = "text/javascript">
09 function countDown (endDate ){
10 var now = new Date ();
11 var deadtime = document. getElementById (endDate );
12 var deadline = new Date (deadtime. value );
13 // The minute difference between the local time and Greenwich Mean Time (GMT)
14 var timeDiff = now. getTimezoneOffset ();
15 // there is some confusion here. If the minute difference is converted into milliseconds, it should be timeDiff * 60*1000, but the returned data is incorrect!
16 var leave = Math. abs (deadline. getTime ()-now. getTime () + timeDiff * 60 );
17 var minute = 1000*60;
18 var hour = minute * 60;
19 var day = hour * 24;
20 var countDay = Math. floor (leave/day );
21 // var countHour = Math. floor (leave-day * countDay)/hour); two calculation ideas
22 var countHour = Math. floor (leave/hour-countDay * 24 );
23 var countMinute = Math. floor (leave/minute)-countDay * 24*60-countHour * 60;
24 var countSecond = Math. floor (leave/1000)-countDay * 24*60*60-countHour * 60*60-countMinute * 60;
25 var outStr = "";
26 if (deadline <now ){
27 outStr = "distance" + deadtime. value + "existing" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
28} else {
29 outStr = "distance" + deadtime. value + "poor" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
30}
31 var showTime = document. getElementById ("showTime ");
32 showTime. innerHTML = outStr;
33}
34 </script>
35
36
37 <body>
38 <p>
39 <label for = "deadline"> deadline </label>
40 <input type = "text" value = "2011-06-11" id = "deadline"/>
41 <input type = "button" value = "Start countDown" onclick = "window. setInterval (countDown (deadline), 1000)"/> </p>
42 <p id = "showTime"> </p>
43 </body>
44
Some html code:
You can enter the countdown time in the text box. If the current time is less than the deadline, the system returns normally. Otherwise, the system returns the time from the deadline to the present.
View sourceprint? 1 <p>
2 <label for = "deadline"> deadline </label>
3 <input type = "text" value = "2011-06-11" id = "deadline"/>
4 <input type = "button" value = "Start countDown" onclick = "window. setInterval (countDown (deadline), 1000)"/>
5 </p>
6 <p id = "showTime"> </p>
Javascript code:
View sourceprint? 01 function countDown (endDate ){
02 var now = new Date ();
03 var deadtime = document. getElementById (endDate );
04 var deadline = new Date (deadtime. value );
05 // The minute difference between the local time and Greenwich Mean Time (GMT)
06 var timeDiff = now. getTimezoneOffset ();
07 // there is some confusion here. If the minute difference is converted into milliseconds, it should be timeDiff * 60*1000, but the returned data is incorrect!
08 var leave = Math. abs (deadline. getTime ()-now. getTime () + timeDiff * 60 );
09 var minute = 1000*60;
10 var hour = minute * 60;
11 var day = hour * 24;
12 var countDay = Math. floor (leave/day );
13 // var countHour = Math. floor (leave-day * countDay)/hour); two calculation ideas
14 var countHour = Math. floor (leave/hour-countDay * 24 );
15 var countMinute = Math. floor (leave/minute)-countDay * 24*60-countHour * 60;
16 var countSecond = Math. floor (leave/1000)-countDay * 24*60*60-countHour * 60*60-countMinute * 60;
17 var outStr = "";
18 if (deadline <now ){
19 outStr = "distance" + deadtime. value + "existing" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
20} else {
21 outStr = "distance" + deadtime. value + "poor" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
22}
23 var showTime = document. getElementById ("showTime ");
24 showTime. innerHTML = outStr;
25}