Files

111 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

/*
* Created by Strawmanbobi
* 2014-08-30
*/
function formatDate(date, fmt) {
let o = {
"M+" : date.getMonth() + 1,
"d+" : date.getDate(),
"h+" : date.getHours(),
"m+" : date.getMinutes(),
"s+" : date.getSeconds(),
"q+" : Math.floor((date.getMonth() + 3) / 3),
"S" : date.getMilliseconds()
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
for(let k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
function getNextDay(dateString) {
let nextDayStr;
let nextMonthStr;
let nextYearStr;
let currentDate = new Date(dateString);
let nextDate = new Date(currentDate.getTime() + 24 * 60 * 60 * 1000);
let nextDay = nextDate.getDate();
if(nextDay < 10) {
nextDayStr = "0" + nextDay;
} else {
nextDayStr = nextDay;
}
let nextMonth = nextDate.getMonth() + 1;
if(nextMonth < 10) {
nextMonthStr = "0" + nextMonth;
} else {
nextMonthStr = nextMonth;
}
nextYearStr = nextDate.getFullYear();
return nextYearStr + "-" + nextMonthStr + "-" + nextDayStr;
}
function getPreviousDay(dateString) {
let lastDayStr;
let lastMonthStr;
let lastYearStr;
let currentDate = new Date(dateString);
let lastDate = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000);
let lastDay = lastDate.getDate();
if(lastDay < 10) {
lastDayStr = "0" + lastDay;
} else {
lastDayStr = lastDay;
}
let lastMonth = lastDate.getMonth() + 1;
if(lastMonth < 10) {
lastMonthStr = "0" + lastMonth;
} else {
lastMonthStr = lastMonth;
}
lastYearStr = lastDate.getFullYear();
return lastYearStr + "-" + lastMonthStr + "-" + lastDayStr;
}
function getDateDiffer(startTime, endTime, diffType) {
startTime = startTime.replace(/\-/g, "/");
endTime = endTime.replace(/\-/g, "/");
diffType = diffType.toLowerCase();
let sTime = new Date(startTime);
let eTime = new Date(endTime);
let divNum = 1;
switch (diffType) {
case "second":
divNum = 1000;
break;
case "minute":
divNum = 1000 * 60;
break;
case "hour":
divNum = 1000 * 3600;
break;
case "day":
divNum = 1000 * 3600 * 24;
break;
default:
break;
}
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
function dateDiff(sDate1, sDate2) {
let aDate, oDate1, oDate2, iDays;
aDate = sDate1.split("-");
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式
aDate = sDate2.split("-");
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
return iDays;
}
exports.getNextDay = getNextDay;
exports.getPreviousDay = getPreviousDay;
exports.getDateDiffer = getDateDiffer;
exports.formatDate = formatDate;
exports.dateDiff = dateDiff;