[removed] get all months between two dates?

后端 未结 10 653
醉酒成梦
醉酒成梦 2020-12-09 10:12

I have two date strings like this:

var startDate = \'2012-04-01\';
var endDate = \'2014-11-01\';

And I want to end up with an array of stri

10条回答
  •  一个人的身影
    2020-12-09 10:45

    If loading an extra library isn't a problem, you could always try the awesome MomentJS.
    Gives for very clean and powerful date manipulation.

    var startDate = moment('2012-04-01');
    var endDate = moment('2014-11-01');
    
    var dates = [];
    endDate.subtract(1, "month"); //Substract one month to exclude endDate itself
    
    var month = moment(startDate); //clone the startDate
    while( month < endDate ) {
        month.add(1, "month");
        dates.push(month.format('YYYY-MM-DD'));
    }
    
    console.log(dates);
    

    JSFiddle here

提交回复
热议问题