Online Demo: http://demo.jb51.net/js/2012/mydaojishi/
Packaged Downloads: mydaojishi_jb51
Core code:
Copy Code code as follows:
$ (function () {
var tyear = ""; The year entered
var tmonth = ""; The month entered
var tdate = ""; The date entered
var iremain = ""; The number of milliseconds between start and end
var sdate = ""; Number of days to count backwards
var shour = ""; The hour of the countdown
var smin = ""; Minutes of Countdown
var sSec = ""; Number of seconds to Countdown
var smsec = ""; Number of milliseconds
Generic tool function, add 0 in single-digit digits, and set the front plus a few zeros according to the parameters of the n that is passed.
function Setdig (num,n) {
var str = "" +num;
while (Str.length<n) {
Str= "0" +str
}
return str;
}
Get the difference between days, hours, minutes, seconds
function getdate () {
Date object to create start time and end time
var ostartdate = new Date ();
var oenddate = new Date ();
Get the value of a text box
Tyear = $ ("#tyear"). Val ();
Tmonth = $ ("#tmonth"). Val ();
Tdate = $ ("#tdate"). Val ();
Set End time
Oenddate.setfullyear (parseint (tyear));
Oenddate.setmonth (parseint (Tmonth)-1);
Oenddate.setdate (parseint (tdate));
Oenddate.sethours (0);
Oenddate.setminutes (0);
Oenddate.setseconds (0);
The number of seconds to find the start and end times (divided by 1000)
Iremain = (Oenddate.gettime ()-ostartdate.gettime ())/1000;
The total number of seconds divided by the number of seconds in a day, and then take out the integer part, we can see how many days.
Sdate = Setdig (parseint (iremain/(60*60*24)), 3);
The total number of seconds divided by the number of seconds in a day, and then the remainder, that is, the whole number of days after deduction, the remaining total seconds.
Iremain%= 60*60*24;
The total number of seconds remaining divided by the number of seconds in one hours, and then the integer part, is how many hours.
Shour = Setdig (parseint (iremain/(60*60)), 2)
The remaining total seconds divided by the number of seconds in one hours, and then the remainder, which is the total number of seconds after deducting hours.
Iremain%= 60*60;
The total number of seconds remaining divided by the number of seconds in a minute, and then the integer part, that is, how many minutes.
Smin = Setdig (parseint (IREMAIN/60), 2)
The remaining total seconds divided by a minute of seconds, and then the remainder, the remainder, is the total number of seconds after deducting minutes.
iremain%=60;
The number of seconds left
SSec = Setdig (iremain,2);
Number of milliseconds
Smsec = ssec*100;
}
Change the time displayed
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);
}
});
}
Perform a time update every second
function Autotime () {
GETDATE ();
If less than 0, clear calls themselves, and returns
if (iremain<0) {
Cleartimeout (sett);
Return
}
Updateshow ();
var sett = settimeout (autotime,1000);
}
Click the button to start the timer
$ ("button"). Click (function () {
Autotime ();
})
})
Record where you need to be aware:
1. Modulo operation:
Iremain%= 60*60*24;
is to return the remainder, and the remainder in this instance is the number of seconds left after the integer is taken away.
2. Tool function Setdig (Num,n) can automatically add 0 to the incoming number according to the parameters passed in