Javascript tips-js tutorial

Source: Internet
Author: User
The following small series will bring you an implementation code for js time difference. I think it is quite good. I will share it with you now. I would also like to give you a reference. Let's take a look at var date1 = new Date (); // the start time.
Var date2 = new Date (); // End Time
Var date3 = date2.getTime ()-date1.getTime () // time difference in milliseconds

// Calculate the number of days for difference
Var days = Math. floor (date3/(24*3600*1000 ))
// Calculate the number of hours

Var leave1 = date3 % (24*3600*1000) // The number of milliseconds remaining after the number of computing days
Var hours = Math. floor (leave1/(3600*1000 ))

// Calculate the number of minutes of difference
Var leave2 = leave1 % (3600*1000) // The number of milliseconds remaining after the computing hours
Var minutes = Math. floor (leave2/(60*1000 ))
// Calculate the time difference in seconds
Var leave3 = leave2 % (60*1000) // The number of milliseconds remaining after the number of minutes is calculated
Var seconds = Math. round (leave3/1000)

Alert ("difference" + days + "days" + hours + "hours" + minutes + "minutes" + seconds + "seconds ")

Math. floor (Math. abs (k1-k2)/1000/60/60/24/0.5 +); // time difference rounding

Document. write ("two decimal places:" + a. toFixed (2) +"
Four decimal places "+ a. toFixed (4 ));

The implementation code of the above js time difference is all the content shared by the editor. I hope it can be used as a reference for you and also as a reference for your support.

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.