Negin Basiri
Negin Basiri

Reputation: 1375

Finding month name and year between two dates

I have a date range picker and I want to find out the name of months in between start date and end date.

for example lets say user selected start date 12-Nov-2014 and end date 13-Apr-2015 so what I want is having an array like below ['Nov 14', 'Dec 14', 'Jan 15', 'Feb 15', 'Mar 15', 'Apr 15']

How would be your approach to achieve this? PS: I have momentjs included in project so no problem if your solution is using momet

Upvotes: 2

Views: 2830

Answers (5)

Vladimir
Vladimir

Reputation: 342

5 lines implementation using Moment.js

var startDate = moment('12-Nov-2014', 'DD-MMM-YYYY');
var endDate = moment('13-Apr-2015', 'DD-MMM-YYYY');
var iterationDate = startDate.clone().add(-1, 'month');

var resultArr=[];
while(+(iterationDate.add(1, 'month'))<+endDate.endOf('month')) resultArr.push(iterationDate.format('MMM YY'));
// resultArr : Nov 14,Dec 14,Jan 15,Feb 15,Mar 15,Apr 15

Check out fiddle here.

Upvotes: 1

Abs
Abs

Reputation: 3962

Here's a little snippet that uses moment.js (fiddle):

var d1 = new Date(2015, 10, 27);
var d2 = new Date(2015, 11, 27);

var ydiff = d2.getYear() - d1.getYear();
var mdiff = d2.getMonth() - d1.getMonth();

var diff = (ydiff*12 + mdiff);
var arr=[]
for(i = 0 ; i<=diff;i++){
if(i==0)
    d1.setMonth(d1.getMonth() -1);
else
    d1.setMonth(d1.getMonth() + 1);

    arr[i]= moment(d1).format("MMM YY");
}
alert(arr);

Upvotes: 1

Bhargav Ponnapalli
Bhargav Ponnapalli

Reputation: 9412

How about this? http://codepen.io/bhargav175/pen/EVBZyL

var dates = ['05-12-2014','12-11-2015'];
var diff = dates.map(function(i){
var m = moment(i);

return {
  month : m.month(),
  year : m.year()
};
});

var start = Object.assign({},diff[0]), end= Object.assign({},diff[diff.length-1]);

var monthNames = ["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"];

var months = [];
if(end.year > = start.year){
while(start.month < end.month ||  start.year < end.year){

       if(start.month < 11){
          start.month ++;
       }else{
         start.month = 0;
         start.year++;
       }
       months.push(monthNames[start.month]+" ,"+start.year)
}}
console.log(months);

Upvotes: 1

kailash yogeshwar
kailash yogeshwar

Reputation: 834

You can add month to a date and then get month on each iteration till your start date's year is equal to end date year and on each iteration get the index of month by

getMonthName(date.getMonth()) function getMonthName(index){ //logic }

which will give you the name of month and do further processing.

Upvotes: 1

escargot agile
escargot agile

Reputation: 22389

You could write a loop starting from the 1st of the month of the first date (in your example November 1st) and adding one month in every iteration (so that it's always the 1st of every month) until the iterated date surpasses the end date. For each iteration you add the month to the array.

I prefer starting from the 1st because it deals well with edge cases. For example, if your start from January 31st and add a month, I'm not sure if you're going to jump over February or not.

Upvotes: 1

Related Questions