In practical applications, it is necessary to calculate the gap between the two time points, generally calculate the current time and a specified point of time between the gap, and sometimes need to be accurate to days, hours, minutes and seconds, the following is a brief description of how to achieve this effect.
Effect Chart:
From the New Year:
The code is as follows:
The above code to achieve the function we want, the following is a brief introduction to the implementation of the effect of the process.
I. Principle of realization:
The principle is very simple, is to calculate the millisecond gap between the point of time, and then through the mathematical calculation of the corresponding days, hours, minutes and descriptions, through the setinterval () function called every second function, then is the first inverted meter effect.
Two. Code comments:
1.function Thencethen () {}, this function is used to calculate the time gap.
2.var thetime= "2014/5/4", this variable is used to define a point in time when you want to calculate the difference.
3.var endtime=new Date (thetime) to create the current time object.
4.var totalsecs= (endtime-new Date ())/1000, the difference between two time objects is the millisecond gap between them, and divided by 1000 is the description of the difference.
5.var Days=math.floor (TOTALSECS/3600/24), calculate the difference of days, pay special attention to the function of Math.floor (), can refer to the relevant reading.
6.var Hours=math.floor ((totalsecs-days*24*3600)/3600) to calculate the difference in the number of hours.
The above is the JavaScript computation time difference sample code, hoped for everybody's study to be helpful.