使用 javascript 获取今天之前的所有 90 天

时间:2021-06-15 06:02:29

标签: javascript arrays typescript

我想将今天之前的所有 90 天作为一个数组。我在 StackOverflow 或 Google 中找不到任何解决方案。

 Document document = Document.create();
            Map<String, Object> map = new Phone.ToMap().convert((Phone) value);
            document.put(key, map);

        UpdateQuery updateQuery = UpdateQuery.builder(id).withDocument(document).build();
        UpdateResponse updateResponse = operations.update(updateQuery, indexCoordinates());
        Result result = updateResponse.getResult();

我的预期结果是今天前 90 天的数组:

const now = new Date(); 
const daysBefore = now.setDate(priorDate.getDate() - 90);

1 个答案:

答案 0 :(得分:5)

通过创建一个长度为 90 的新数组并将每个条目映射到一个新日期(now 减去 index 天数)来创建一个日期数组

const now = new Date()
const length = 90

const days = Array.from({ length }, (_, days) => {
  let day = new Date(now) // clone "now"
  day.setDate(now.getDate() - days) // change the date
  return day
})

console.log(days)

这会创建一个 Date 实例数组,以 now 开头并返回 90 天(降序)。

另见Array.from()

相关问题