Tachun Lin
Tachun Lin

Reputation: 972

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

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.

Upvotes: 24

Views: 34862

Answers (7)

vinjenzo
vinjenzo

Reputation: 1530

This should do it:

var startDate = moment('2021-12-31');
var endDate = moment('2022-12-14');
var betweenMonths = [];

if (startDate < endDate){
   var date = startDate.startOf('month');

   while (date < endDate.endOf('month')) {
      betweenMonths.push(date.format('YYYY-MM'));
      date.add(1,'month');
   }
}

Upvotes: 47

Subhojit Ghosh
Subhojit Ghosh

Reputation: 287

This is the best way in my opinion.

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

Upvotes: 1

AndreasT
AndreasT

Reputation: 1001

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>

Upvotes: 2

ZEE
ZEE

Reputation: 5849

Why don't you just 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
// ]

Upvotes: 0

Alexander Swann
Alexander Swann

Reputation: 639

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.

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

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

Upvotes: 35

Raghd Hamzeh
Raghd Hamzeh

Reputation: 349

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.

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>

Upvotes: 4

Naing Lin Aung
Naing Lin Aung

Reputation: 3430

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

Upvotes: 3

Related Questions