Home >Web Front-end >JS Tutorial >js time difference calculation code [including calculation, days, hours, minutes, seconds]_javascript skills

js time difference calculation code [including calculation, days, hours, minutes, seconds]_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:03:521192browse

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 obtained is the number of milliseconds. The time can be judged based on the size of the number of milliseconds.

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

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

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

var date1=new Date(); //Start time
var date2=new Date(); //End time
var date3=date2.getTime()-date1.getTime() //The number of milliseconds of the time difference

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

//Calculate the difference in days
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 calculating the number of days
var hours=Math.floor(leave1/(3600*1000))
//Calculate the difference in minutes
var leave2=leave1%(3600*1000) //The number of milliseconds remaining after calculating the number of hours
var minutes=Math.floor(leave2/(60*1000))
//Calculate the difference in seconds
var leave3=leave2%(60*1000) //The number of milliseconds remaining after calculating the 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 calculation, day, hour, minute, second] is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn