Open GaryHjy opened 4 years ago
function calculateTime(startTime, endTime) {
const startTimestamp = Date.parse(startTime)
const endTimestamp = Date.parse(endTime)
const res = []
let current = 0
if (!startTimestamp || !endTime || (startTimestamp > endTime)) {
throw Error('请输入有效时间')
}
while (startTimestamp + current <= endTimestamp) {
res.push(startTimestamp + current)
current += 3600 * 24 * 1000
}
return res.map(time => {
const dates = new Date(time)
const year = dates.getFullYear()
const month = dates.getMonth() + 1
const day = dates.getDate()
return [year, month, day].join('-')
})
}
function simpleFormatter(timeStamp) {
const date = new Date(timeStamp)
const year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate()
return `${year}-${`${month}`.padStart(2, 0)}-${`${day}`.padStart(2, 0)}`
}
function genarate(startDate, endDate) {
let start = + new Date(startDate), end = + new Date(endDate)
start > end && ([start, end] = [end, start])
const oneDay = 8.64e7, len = Math.ceil((end - start) / oneDay), result = []
for (let i = 0; i <= len; i++) {
result.push(simpleFormatter(start + oneDay * i))
}
return result
}
console.log(genarate('2019-02-12', '2019-04-12')) // ["2019-02-12","2019-02-13","2019-02-14","2019-02-15","2019-02-16","2019-02-17","2019-02-18","2019-02-19","2019-02-20","2019-02-21","2019-02-22","2019-02-23","2019-02-24","2019-02-25","2019-02-26","2019-02-27","2019-02-28","2019-03-01","2019-03-02","2019-03-03","2019-03-04","2019-03-05","2019-03-06","2019-03-07","2019-03-08","2019-03-09","2019-03-10","2019-03-11","2019-03-12","2019-03-13","2019-03-14","2019-03-15","2019-03-16","2019-03-17","2019-03-18","2019-03-19","2019-03-20","2019-03-21","2019-03-22","2019-03-23","2019-03-24","2019-03-25","2019-03-26","2019-03-27","2019-03-28","2019-03-29","2019-03-30","2019-03-31","2019-04-01","2019-04-02","2019-04-03","2019-04-04","2019-04-05","2019-04-06","2019-04-07","2019-04-08","2019-04-09","2019-04-10","2019-04-11","2019-04-12"]
const formatDate = (a = '2015-2-8',b = '2015-3-3', c = []) => {
let d = 0;
const q1 = new Date(a).getTime();
const q2 = new Date(b).getTime();
const dayTime = 3600 * 24 * 1000;
while (d + q1 <= q2) {
const a = new Date(d + q1);
const year = a.getFullYear()
const month = a.getMonth() + 1
const day = a.getDate()
c.push(`${year}-${month}-${day}`);
d += dayTime;
}
return c;
}
console.log(formatDate()); //["2015-2-8", "2015-2-9", "2015-2-10", "2015-2-11", "2015-2-12", "2015-2-13", "2015-2-14", "2015-2-15", "2015-2-16", "2015-2-17", "2015-2-18", "2015-2-19", "2015-2-20", "2015-2-21", "2015-2-22", "2015-2-23", "2015-2-24", "2015-2-25", "2015-2-26", "2015-2-27", "2015-2-28", "2015-3-1", "2015-3-2", "2015-3-3"]