First, check all the complete code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta content = "text/ html; charset = UTF-8 "http-equiv =" Content-Type "/> <title> countDown test </title> </pead> <body> <p> <label for =" deadline "> end date </label> <input type =" text "value =" 2011-06-11 "id =" deadline "/> <input type =" button "value =" Start countdown" onclick = "window. setInterval ('countdown (\ 'deadline \ ')', 1000) "/> </p> <p id =" showTime "> </p> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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.
Copy codeThe Code is as follows:
<P>
<Label for = "deadline"> deadline </label>
<Input type = "text" value = "2011-06-11" id = "deadline"/>
<Input type = "button" value = "Start countDown" onclick = "window. setInterval ('countdown (\ 'adline \ ')', 1000)"/>
</P>
<P id = "showTime"> </p>
Javascript code:
Copy codeThe Code is as follows:
Function countDown (endDate ){
Var now = new Date ();
Var deadtime = document. getElementById (endDate );
Var deadline = new Date (deadtime. value );
// The minute difference between the local time and Greenwich Mean Time (GMT)
Var timeDiff = now. getTimezoneOffset ();
// 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!
Var leave = Math. abs (deadline. getTime ()-now. getTime () + timeDiff * 60 );
Var minute = 1000*60;
Var hour = minute * 60;
Var day = hour * 24;
Var countDay = Math. floor (leave/day );
// Var countHour = Math. floor (leave-day * countDay)/hour); two calculation ideas
Var countHour = Math. floor (leave/hour-countDay * 24 );
Var countMinute = Math. floor (leave/minute)-countDay * 24*60-countHour * 60;
Var countSecond = Math. floor (leave/1000)-countDay * 24*60*60-countHour * 60*60-countMinute * 60;
Var outStr = "";
If (deadline <now ){
OutStr = "distance" + deadtime. value + "existing" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
} Else {
OutStr = "distance" + deadtime. value + "poor" + countDay + "day" + countHour + "Hour" + countMinute + "Minute" + countSecond + "second ";
}
Var showTime = document. getElementById ("showTime ");
ShowTime. innerHTML = outStr;
}