Online Demo: http://demo.jb51.net/js/2012/mydaojishi/
Package and download: mydaojishi_jb51
Core code:
Copy codeThe Code is as follows:
$ (Function (){
Var tYear = ""; // input year
Var tMonth = ""; // The input month
Var tDate = ""; // input date
Var iRemain = ""; // Number of milliseconds between start and end
Var sDate = ""; // counting days
Var sHour = ""; // countdown hour
Var sMin = ""; // minutes of countdown
Var sSec = ""; // countdown seconds
Var sMsec = ""; // Number of milliseconds
// Common tool function. Add zero to the single digit. Based on the passed N parameter, add the first few zeros.
Function setDig (num, n ){
Var str = "" + num;
While (str. length <n ){
Str = "0" + str
}
Return str;
}
// Obtain the day, hour, minute, and second of the difference
Function getdate (){
// Create a date object with the start time and End Time
Var oStartDate = new Date ();
Var oEndDate = new Date ();
// Obtain the value of the text box
TYear = $ ("# tyear"). val ();
TMonth = $ ("# tmonth"). val ();
TDate = $ ("# tdate"). val ();
// Set the end time
OEndDate. setFullYear (parseInt (tYear ));
OEndDate. setMonth (parseInt (tMonth)-1 );
OEndDate. setDate (parseInt (tDate ));
OEndDate. setHours (0 );
OEndDate. setMinutes (0 );
OEndDate. setSeconds (0 );
// Obtain the start time and end time in seconds (divided by 1000)
IRemain = (oEndDate. getTime ()-oStartDate. getTime ()/1000;
// The total number of seconds is divided by the number of seconds in a day. Then, the number of days is obtained by taking out the integer.
SDate = setDig (parseInt (iRemain/(60*60*24), 3 );
// The total number of seconds divided by the number of seconds of the day, and then the remainder is the total number of seconds after the integer day is deducted.
IRemain % = 60*60*24;
// The remaining total number of seconds is divided by the number of seconds in an hour, and then an integer is used to determine the number of hours.
SHour = setDig (parseInt (iRemain/(60*60), 2)
// The remaining total number of seconds is divided by the number of seconds in one hour, and then the remaining number is obtained. The remainder is the total number of seconds after the hour is deducted.
IRemain % = 60*60;
// The remaining total number of seconds is divided by the number of seconds in one minute, and then the integer is used to determine the number of minutes.
SMin = setDig (parseInt (iRemain/60), 2)
// The remaining total number of seconds divided by the number of seconds in one minute, and then the remaining number. The remainder is the total number of seconds after the minute is deducted.
IRemain % = 60;
// The remaining seconds
SSec = setDig (iRemain, 2 );
// Millisecond count
SMsec = sSec * 100;
}
// Change the display time
Function updateShow (){
$ (". Showdate span"). text (tYear + "-" + tMonth + "-" + tDate );
$ (". Count span"). each (function (index, element ){
If (index = 0 ){
$ (This). text (sDate );
} Else if (index = 1 ){
$ (This). text (sHour );
} Else if (index = 2 ){
$ (This). text (sMin );
} Else if (index = 3 ){
$ (This). text (sSec );
} Else if (index = 4 ){
$ (This). text (sMsec );
}
});
}
// Update the execution time every second
Function autoTime (){
Getdate ();
// If the value is less than zero, clear the caller and return
If (iRemain <0 ){
ClearTimeout (setT );
Return;
}
UpdateShow ();
Var setT = setTimeout (autoTime, 1000 );
}
// Click the button to start timing
$ ("Button"). click (function (){
AutoTime ();
})
})
Notes:
1. modulo operation:
IRemain % = 60*60*24;
Returns the remainder. The remainder in this instance is the number of seconds after the integer is taken away.
2. The tool function setDig (num, n) can automatically add zero before the input number based on the input parameters.