JS Calculation Time Difference Code "includes calculations, days, hours, minutes, seconds" _javascript tips

Source: Internet
Author: User
Tags date1

var Begintime_ms = date.parse (New Date (Begintime.replace (/-/g, "/")); BeginTime to start time

var Endtime_ms = date.parse (New Date (Endtime.replace (/-/g, "/")); Endtime to end time

The result is a millisecond number, which can be used to determine the time according to the size of the millisecond.

Of course, according to the number of milliseconds can be based on their difference to find the difference between the number of days or hours.

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

The above is the number of milliseconds to obtain the user input time

var date1=new Date (); Start time
var date2=new Date (); End time
var date3=date2.gettime ()-date1.gettime ()//Time difference milliseconds

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

Calculate the difference in days
var Days=math.floor (date3/(24*3600*1000))

Calculate the number of hours

var leave1=date3% (24*3600*1000)//number of milliseconds remaining after counting days
var Hours=math.floor (leave1/(3600*1000))
Calculate the difference of minutes
var leave2=leave1% (3600*1000)//number of milliseconds remaining after calculating hours
var Minutes=math.floor (leave2/(60*1000))
Count the difference in seconds
var leave3=leave2% (60*1000)//number of milliseconds remaining after counting minutes
var seconds=math.round (leave3/1000)
Alert ("Difference" +days+ "Days" +hours+ "hours" +minutes+ "minutes" +seconds+ "seconds")

The above JS calculation time difference code "including calculations, days, hours, minutes, seconds" is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.