首页 > 解决方案 > Moment.js 获取最近 28 天

问题描述

我正在使用 React-Native。Moment.js 中是否有任何方法可以获取从现在开始的最后 28 天?我看到了一个插件moment-range,但我不想安装它。

基本上我需要的是过去 28 天的数组。

例子:

const now = moment()
const lastMonth = moment.subtract(28, 'days')
const lastMonthArr = [1,2....28]

标签: momentjs

解决方案


你可以尝试这样的事情:

let now = moment();
let pastDates = [];
for( let i=1;i<=28;i++) { 
 now = now.subtract(1, 'day')
  console.log(now.format('MMM Do YY'))
  pastDates.push(now)
}

推荐阅读