如何用moment.js列出2个日期之间的所有月份?

时间:2015-04-06 07:11:27

标签: javascript momentjs

' ave两个日期

2015-3-30      2013-8-31

如何制作一个月份列表:

[ '2015-3', '2015-2', '2015-1', '2014-12', '2014-11', '2014-10', '2014-09', '2014-08', '2014-07', '2014-06', '2014-05'....., '2013-08' ] 

感谢。

7 个答案:

答案 0 :(得分:31)

这应该这样做:

var dateStart = moment('2013-8-31');
var dateEnd = moment('2015-3-30');
var timeValues = [];

while (dateEnd > dateStart || dateStart.format('M') === dateEnd.format('M')) {
   timeValues.push(dateStart.format('YYYY-MM'));
   dateStart.add(1,'month');
}

答案 1 :(得分:15)

我认为原始答案并不完全正确,因为您不会在阵列中获得MyObject(String sn) { if (sn == null || sn.length() < 7 || sn.length() > 9) { throw new IllegalArgumentException("sn"); } serialNumber = sn; } 。这是因为您的开始日期最终会以'2015-3'结束,并且会使条件失败。您可以像下面一样扩展它。

'2015-3-31'

答案 2 :(得分:3)

您在输出中使用多种格式:YYYY-MM和YYYY-M,所以我选择了第一种格式。您可以根据需要进行编辑。

var startDateString = "2012-5-30";
var endDateString = "2015-8-31";
var startDate = moment(startDateString, "YYYY-M-DD");
var endDate = moment(endDateString, "YYYY-M-DD").endOf("month");

var allMonthsInPeriod = [];

while (startDate.isBefore(endDate)) {
  allMonthsInPeriod.push(startDate.format("YYYY-MM"));
  startDate = startDate.add(1, "month");
};

console.log(allMonthsInPeriod);

document.getElementById("result").innerHTML = allMonthsInPeriod.join("<br />");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js"></script>
<div id="result"></div>

答案 3 :(得分:1)

您可以尝试使用此示例

var one = moment("2015-3-30");
  var two = moment("2014-8-31");
  var dateDiffs = [];
  var count = Math.round(moment.duration(one.diff(two)).asMonths());

  month =  two.month() + 1;
  year  =  two.year();

  for (var i=1; i<=count; i++) {
      if (month > 12) {
        month = 1;
        year++;
      }
      dateDiffs.push(year+"-"+month);
      console.log(month);
      month++;
  }

  console.log(dateDiffs);

答案 4 :(得分:1)

const getMonths = (start, end) =>
    Array.from({ length: end.diff(start, 'month') + 1 }).map((_, index) =>
      moment(start).add(index, 'month').format('MM.YYYY'),
    );
    
const months = getMonths(moment('01.2019','MM.YYYY'),moment('01.2020','MM.YYYY'))
    
console.log(months)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js"></script>

答案 5 :(得分:0)

你为什么不https://date-fns.org/v2.13.0/docs/eachMonthOfInterval

// Each month between 6 February 2014 and 10 August 2014:
var result = eachMonthOfInterval({
  start: new Date(2014, 1, 6),
  end: new Date(2014, 7, 10)
})
//=> [
//   Sat Feb 01 2014 00:00:00,
//   Sat Mar 01 2014 00:00:00,
//   Tue Apr 01 2014 00:00:00,
//   Thu May 01 2014 00:00:00,
//   Sun Jun 01 2014 00:00:00,
//   Tue Jul 01 2014 00:00:00,
//   Fri Aug 01 2014 00:00:00
// ]

答案 6 :(得分:0)

我认为这是最好的方法。

const startDate = moment('2013-8-31', 'YYYY-M-DD');
const endDate = moment('2015-3-30', 'YYYY-M-DD');

const months = [];
const flag = startDate;
while (flag.diff(endDate) <= 0) {
  months.push(flag.format('YYYY-M'));
  flag.add(1, 'M');
}