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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29466944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:34:52  来源:igfitidea点击:

How to list all month between 2 dates with moment.js?

javascriptmomentjs

提问by Tachun Lin

I've two dates

我有两个约会

2015-3-30      2013-8-31

How can I make a month list like:

我如何制作一个月的清单,例如:

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

Thanks.

谢谢。

回答by vinjenzo

This should do it:

这应该这样做:

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');
}

回答by Alexander Swann

I think the original answer isn't entirely correct, as you wouldn't get '2015-3'in your array. This is due to the fact your start date would eventually end up as '2015-3-31'and would fail the conditional in place. You could extend it like below.

我认为原始答案并不完全正确,因为您不会进入'2015-3'数组。这是因为您的开始日期最终会以这样的方式结束,'2015-3-31'并且会在适当的条件下失败。你可以像下面那样扩展它。

UPDATE: I've now included cloning the dateStart variable so it isn't mutated at all.

更新:我现在已经包括克隆 dateStart 变量,因此它根本不会发生突变。

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

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

回答by Raghd Hamzeh

You are using multiple formats in the output: YYYY-MM and YYYY-M, so I picked the first. You can edit as you see fit.

您在输出中使用了多种格式: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>

回答by Naing Lin Aung

You could try with this example

你可以试试这个例子

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);

回答by ZEE

Why don't you just https://date-fns.org/v2.13.0/docs/eachMonthOfInterval??

你为什么不只是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
// ]