Share the sample code for calculating the time difference in javascript and the sample code in javascript.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.