如何在MomentJS中获取上个月,最近三个月的日期列表

时间:2019-01-08 08:55:08

标签: javascript momentjs

使用moment().subtract(1, 'days').format("YYYY-MM-DD"),我可以从当前日期过去x天。那么我怎样才能获得上个月或过去三个月的所有日子呢?

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,可以通过两个步骤来解决。首先计算标记上个月开始日期的时刻:

var prevMonth = moment().subtract(1, 'month').startOf('month');
var prevMonthDays = prevMonth.daysInMonth();

然后,在范围0...prevMonthDays中进行迭代,计算相对于上个月prevMonth的该日期的每天日期:

  var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");

类似的东西应该可以满足您的要求:

// Get moment at start date of previous month
var prevMonth = moment().subtract(1, 'month').startOf('month');
var prevMonthDays = prevMonth.daysInMonth();

// Array to collect dates of previous month
var prevMonthDates = [];

for (var i = 0; i < prevMonthDays; i++) {

  // Calculate moment based on start of previous month, plus day offset
  var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");
  
  prevMonthDates.push(prevMonthDay);
}

console.log(prevMonthDates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

希望这会有所帮助!

更新

要计算从过去一个月到当前月初的日期列表,可以将上面的代码概括如下:

var monthsPast = 4;
var prevMonthDays = 0;
var prevMonth = moment();

// Iterate over number of months past that we want to collect dates for
for (var i = 0; i < monthsPast; i++) {

  // Calculate the moment at the start of a previous month
  var prevMonthStart = moment().subtract(i + 1, 'month').startOf('month');
  
  // Increment total range to collect dates over, and update prevMonth
  // to current calculated moment which represents oldest month start
  prevMonthDays += prevMonthStart.daysInMonth();
  prevMonth = prevMonthStart;
}

// Array to collect dates of previous month
var prevMonthDates = [];

for (var i = 0; i < prevMonthDays; i++) {

  // Calculate moment based on start of previous month, plus day offset
  var prevMonthDay = prevMonth.clone().add(i, 'days').format("YYYY-MM-DD");
  
  prevMonthDates.push(prevMonthDay);
}

console.log(prevMonthDates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

答案 1 :(得分:1)

这是获取当月天数的方法:

const numberOfDaysInCurrentMonth = moment().daysInMonth();

因此,这是获取上个月的天数的方法:

const numberOfDaysInLastMonth = moment().subtract(1, 'months').daysInMonth();

现在您需要上个月的开始:

const startOfLastMonth = moment().subtract(1, 'months').startOf('month');

您已经准备好开始迭代并建立列表:

const numberOfDaysInLastMonth = moment().subtract(1, 'months').daysInMonth();
const startOfLastMonth = moment().subtract(1, 'months').startOf('month');

for (let i = 0; i < numberOfDaysInLastMonth; i = i + 1) {
  console.log(startOfLastMonth.format('YYYY MMM DD'));
  startOfLastMonth.add(1, 'days');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

在过去3个月内,只需更改您的开始日期和天数:

const numberOfDaysInLast3Months = moment().subtract(1, 'months').daysInMonth() +
  moment().subtract(2, 'months').daysInMonth() +
  moment().subtract(3, 'months').daysInMonth();
const startDate = moment().subtract(3, 'months').startOf('month');

for (let i = 0; i < numberOfDaysInLast3Months; i = i + 1) {
  console.log(startDate.format('YYYY MMM DD'));
  startDate.add(1, 'days');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>