Js computing time difference code [including calculation, day, hour, minute, second] _ javascript tips-js tutorial

Source: Internet
Author: User
Tags date1 time in milliseconds
The following small series will bring you a js computing time difference code, including computing, day, hour, minute, and second ]. I think this is quite good. Now I will share it with you and give you a reference for var begintime_ms = Date. parse (new Date (begintime. replace (/-/g, "/"); // begintime is the start time.

Var endtime_ms = Date. parse (new Date (endtime. replace (/-/g, "/"); // endtime is the end time

The result is the number of milliseconds. The time can be determined based on the number of milliseconds.

Of course, you can calculate the number of days or hours of difference based on the number of milliseconds.

-------------------------------

The preceding figure shows the user input time in milliseconds.

Var date1 = new Date (); // 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 ")

The above js computing time difference code, including computing, day, hour, minute, and second, is all the content that xiaobian shares with you. I hope to give you a reference, I also hope that you will support your feet.

Related Article

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.