[removed] get all months between two dates?

后端 未结 10 650
醉酒成梦
醉酒成梦 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:38

    You can also use the excellent moment.js library:

    var startDate = moment('2012-04-01');
    var endDate = moment('2014-11-01');
    
    var result = [];
    
    if (endDate.isBefore(startDate)) {
        throw "End date must be greated than start date."
    }      
    
    while (startDate.isBefore(endDate)) {
        result.push(startDate.format("YYYY-MM-01"));
        startDate.add(1, 'month');
    }
    

    JSFiddle

提交回复
热议问题