/**
*
- @param {any} oneTime 开始时间 yyyy-mm-dd hh24:min:sec
- @param {any} twoTime 结束时间 yyyy-mm-dd hh24:min:sec
- @param {any} stype 返回类型:
1.SEC:相差秒数,
2.MIN:相差分钟数,
3.HH:相差小时数,
4.HH:MIN:SEC:相差 “时:分:秒” 数,
5.DD:相差天数,
6.MM:相差月数,
7.YY:相差年数。
*/
function dateTimeDifference(oneTime, twoTime, stype) {
if (oneTime == null || oneTime == "") return 0;
if (twoTime == null || twoTime == "") return 0;
var myoneTime = new Date(oneTime.replace(/-/g, "/"));
var mytwoTime = new Date(twoTime.replace(/-/g, "/"));
if (myoneTime == null || myoneTime == "") return 0;
if (mytwoTime == null || mytwoTime == "") return 0;
if (myoneTime > mytwoTime) return 0;
if (stype == "SEC") {
return (mytwoTime - myoneTime) / 1000 + "sec";
} else if (stype == "MIN") {
return ((mytwoTime - myoneTime) / 1000) / 60 + "min";
} else if (stype == "HH") {
return (((mytwoTime - myoneTime) / 1000) / 60) / 60 + "h";
} else if (stype == "DD") {
return ((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24 + "D";
} else if (stype == "MM") {
return (((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24) / 31 + "M";
} else if (stype == "YY") {
return ((((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24) / 31) / 365 + "Y";
} else if (stype == "HH:MIN:SEC") {
var seconds = (mytwoTime - myoneTime) / 1000;
if (seconds <= 60) {
return seconds + "sec";
} else if (60 < seconds <= 3600) {
return Math.floor(seconds / 60) + ":" + (seconds % 60) + "min";
} else if (3600 < seconds <= 216000) {
return Math.floor(seconds / 3600) + ":" + Math.floor((seconds % 3600) / 60) + ":" + ((seconds % 3600) % 60) + "h";
}
} else {
return mytwoTime - myoneTime;
}
}