Home >Web Front-end >JS Tutorial >Use javascript to calculate the number of days between two dates_javascript skills

Use javascript to calculate the number of days between two dates_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:10:112035browse

In ASP, you can use DateDiff to calculate the difference between two times, but it is not available in javascript and requires a handwritten function.
js can actually directly Date1-Date2, and it can be converted into milliseconds to calculate the time difference.


Copy code The code is as follows:

// Calculate two dates The number of days between
function DateDiff(sDate1, sDate2){ //sDate1 and sDate2 are in 2002-12-18 format
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] '-' aDate[2] '-' aDate[0]) //Convert to 12-18-2002 format
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] '-' aDate[2] '-' aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24 ) //Convert the difference in milliseconds to days
return iDays
}
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