let obj = {
getDate(datestr) {
let temp = datestr.split('-')
let date = new Date(temp[0], temp[1] - 1, temp[2])
return date
},
formatEveryDay(start, end, Spacer) {
let dateList = []
let startTime = this.getDate(start)
let endTime = this.getDate(end)
while ((endTime.getTime() - startTime.getTime()) >= 0) {
let year = startTime.getFullYear()
let month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1
let day = startTime.getDate().toString().length == 1 ? '0' + startTime.getDate() : startTime.getDate()
if (Spacer) {
// 如果不需要间隔符则直接拼接返回
if (Spacer === 'null') {
dateList.push(year + month + day) // 如20220422,20220423
} else {
// 需要自定义间隔符
dateList.push(year + Spacer + month + Spacer + day)
}
} else {
dateList.push(year + '-' + month + '-' + day) // 2022-04-22,2022-04-23
}
startTime.setDate(startTime.getDate() + 1)
}
return dateList
}
}
console.log(obj.getDate('2022-01-10')); //"2022-01-09T16:00:00.000Z"
console.log(obj.formatEveryDay('2022-01-10', '2022-01-20'));
console