/*
* 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