[removed] get all months between two dates?

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

    You are handling "logical" jumps, so you doesn't actually need timing arthmetics. So this is a simple counting problem:

    var startDate = '2012-04-01';
    var endDate = '2014-11-01';
    var dates = [];
    
    var d0 = startDate.split('-');
    var d1 = endDate.split('-');
    
    for (
        var y = d0[0];
        y <= d1[0];
        y++
    ) {
        for (
            var m = d0[1];
            m <= 12;
            m++
        ) {
            dates.push(y+"-"+m+"-1");
            if (y >= d1[0] && m >= d1[1]) break;
        };
        d0[1] = 1;
    };
    
    console.log(dates);
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 10:46

    This is my solution, with help of math and O(n)

    determineMonthInInterval(startDate, endDate) {
    
        let startYear = startDate.getFullYear();
        let endYear = endDate.getFullYear();
    
        let startMonth = startDate.getMonth() + 1;
        let endMonth = endDate.getMonth() + 1;
    
        let monthAmount = (endMonth - startMonth) + 1 + (12 * (endYear - startYear));
    
        let dates = [];
        let currMonth = startMonth;
        let currYear = startYear;
        for( let i=0; i<monthAmount; i++){
            
            let date = new Date(currYear + "/"+currMonth+"/1");
            dates.push(date);
    
            currYear = startYear + Math.floor((startMonth+i) / 12);
            currMonth = (currMonth) % 12 +1;
    
        }
    
        return dates;
    }
    
    0 讨论(0)
  • 2020-12-09 10:48

    This should produce the desired output:

    function dateRange(startDate, endDate) {
      var start      = startDate.split('-');
      var end        = endDate.split('-');
      var startYear  = parseInt(start[0]);
      var endYear    = parseInt(end[0]);
      var dates      = [];
    
      for(var i = startYear; i <= endYear; i++) {
        var endMonth = i != endYear ? 11 : parseInt(end[1]) - 1;
        var startMon = i === startYear ? parseInt(start[1])-1 : 0;
        for(var j = startMon; j <= endMonth; j = j > 12 ? j % 12 || 11 : j+1) {
          var month = j+1;
          var displayMonth = month < 10 ? '0'+month : month;
          dates.push([i, displayMonth, '01'].join('-'));
        }
      }
      return dates;
    }
    

    Just call it with your existing date format:

    dateRange('2013-11-01', '2014-06-01')
    // ["2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01"]
    
    0 讨论(0)
提交回复
热议问题