如何在两个约会之间列出一个月的时间?

时间:2022-09-13 21:27:33

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.

谢谢。

4 个解决方案

#1


25  

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

#2


8  

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”结束,并且会在适当的地方失败条件。你可以把它延伸到下面。

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

#3


3  

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.

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

#4


1  

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

#1


25  

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

#2


8  

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”结束,并且会在适当的地方失败条件。你可以把它延伸到下面。

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

#3


3  

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.

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

#4


1  

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