天數(shù)時(shí)間段:
// 映射日期格式 結(jié)果為: YYYY-MM-DD
function customFormat(date) {
let str = "";
let month = date.getMonth() + 1;
let day = date.getDate();
str += date.getFullYear().toString() + "-";
str += month.toString().padStart(2, '0') + "-";
str += day.toString().padStart(2, '0');
return str;
}
function getDateBetWeen(startDate, endDate) { // 參數(shù)日期格式: YYYY-MM-DD
if (!startDate || !endDate) return [];
let result = [],
startList = startDate.split('-'),
ndList = endDate.split('-'),
date1 = new Date(),
date2 = new Date(),
date1Time,
date2Time;
date1.setUTCFullYear(startList[0], startList[1] - 1, startList[2]);
date2.setUTCFullYear(endList[0], endList[1] - 1, endList[2]);
date1Time = date1.getTime();
date2Time = date2.getTime();
for(let time = date1Time; time <= date2Time;){
console.log(moment(time).format('YYYY-MM-DD'));
result.push(customFormat(new Date(parseInt(time.toString()))));
// 如果項(xiàng)目使用了 moment.js,無(wú)需使用 customFormat 方法
// result.push(moment(time).format('YYYY-MM-DD'));
time = time + 24 * 60 * 60 * 1000;
}
}
月數(shù)時(shí)間段:
function getMonthAll(startMonth, endMonth) { // 參數(shù)日期格式:YYYY-MM
if (!startMonth || !endMonth) return [];
let result = [],
startList = startMonth.split("-"),
endList = endMonth.split("-"),
mCount = 0;
// 計(jì)算開(kāi)始日期、結(jié)束日期之間的月數(shù)
if (parseInt(startList[0]) < parseInt(endList[0])) { // 非同一年
mCount = (parseInt(endList[0]) - parseInt(startList[0])) * 12 + parseInt(endList[1]) - parseInt(startList[1]) + 1;
} else {
mCount = parseInt(endList[1]) - parseInt(startList[1]) + 1;
}
if (mCount > 0) {
let startY = parseInt(startList[0]);
let startM = parseInt(startList[1]);
for (let i = 0; i < mCount; i++) {
result[i] = startY + "-" + startM.toString().padStart(2, '0');
if (startM < 12) {
startM += 1;
} else {
startM = 1;
startY += 1;
}
}
}
return result;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。