Home > Web Front-end > JS Tutorial > Javascript function sharing for calculating time difference_javascript skills

Javascript function sharing for calculating time difference_javascript skills

WBOY
Release: 2016-05-16 18:05:13
Original
1211 people have browsed it
Copy code The code is as follows:

/*
* Get the time difference, the time format is year-month -Day hour: minute: second or year/month/day hour: minute: second
* Among them, year, month and day are in full format, for example: 2010-10-12 01:00:00
* The return precision is : seconds, minutes, hours, days
*/
function GetDateDiff(startTime, endTime, diffType) {
//Convert the time format of xxxx-xx-xx to xxxx/xx/xx format
startTime = startTime.replace(/-/g, "/");
endTime = endTime.replace(/-/g, "/");
//Convert calculated interval characters For lower case
diffType = diffType.toLowerCase();
var sTime = new Date(startTime); //Start time
var eTime = new Date(endTime); //End time
// Number as divisor
var divNum = 1;
switch (diffType) {
case "second":
divNum = 1000;
break;
case "minute": break;
case "hour":
divNum = 1000 * 3600;
break;
case "day":
divNum = 1000 * 3600 * 24;
break;
default:
break;
}
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}

Calling the method is also very simple:
GetDateDiff("2010-10-11 00:00:00", "2010-10-11 00:01:40", "day ")
This is the number of days
GetDateDiff("2010-10-11 00:00:00", "2010-10-11 00:01:40", "seond") is the number of seconds
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template