假设我有一个 ISO 日期,例如2021-09-18T20:18:27.000Z
- 我想以数组的形式获取过去一周的日期
- 该数组应该是一组数组,其中包含两个 ISO 日期,分别代表一天的开始和结束,如下图所示:
例如
输入:
2021-09-18T20:18:27.000Z
输出:
[
['"2021-09-11T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-12T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-13T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-14T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-15T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-16T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-17T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'],
['"2021-09-18T00:00:00.000Z', '"2021-09-18T23:59:59.000Z'],
]
我已经用 dayjs 试过了,这会导致数组表示从特定日期开始的确切间隔:
function getDates(date) {
var dates = [date]
var noOfDays = 7
for (let i = noOfDays - 1; i >= 0; i--) {
const elementIndex = i - noOfDays // Get last nth element of the list
const dateToIncrement = dates.slice(elementIndex)[0]
const newDate = dayjs(dateToIncrement).subtract(1, "day").toISOString()
dates.unshift(newDate)
}
return dates
}
谢谢