Share the sample code for calculating the time difference in javascript and the sample code in javascript.
In practical applications, the gap between two time points needs to be calculated. Generally, the gap between the current time and a specified time point is calculated, and sometimes it needs to be accurate to days, hours, minutes, And seconds. The following describes how to achieve this effect.
:
Distance from New Year:
The Code is as follows:
<Html>
The above code implements the functions we want. The following briefly introduces the implementation process of this effect.
I. Implementation principle:
The principle is very simple, that is, to calculate the millisecond gap between a connection time point, and then calculate the corresponding day, hour, minute, and description through mathematical operations. The second function is called through the setInterval () function, the result is calculated first.
Ii. Code comments:
1. function thenceThen () {}, which is used to calculate the time gap.
2. var theTime = "2014/5/4", which defines a time point for calculating the time difference.
3. var endTime = new Date (theTime) to create the current time object.
4. var totalSecs = (endTime-new Date ()/1000. The difference between the two time objects is the millisecond gap between the two objects. Dividing by 1000 is the difference description.
5. var days = Math. floor (totalSecs/3600/24), calculate the number of days of the difference, pay special attention to the role of the Math. floor () function. For more information, see.
6. var hours = Math. floor (totalSecs-days * 24*3600)/3600), calculate the hours of the difference.
The above is the sample code for calculating the time difference in javascript. I hope it will be helpful for your learning.
Articles you may be interested in:
- How to calculate time difference in JSP
- Function sharing of time difference in Javascript computing
- Js get time difference remove implementation code on Saturday and Sunday
- Jquery js obtains the time difference and time format code
- Self-written Javascript time difference calculation function
- JS and jquery code instances that implement time difference display several minutes ago, several hours ago, and a few days ago