js获取两个时间之间的年月

//获取两个时间段之间的所有年月

function getMonthBetween(start,end){  
    var result = [];  
    var s = start.split("-");  
    var e = end.split("-");  
    var min = new Date();  
    var max = new Date();  
    min.setFullYear(s[0],s[1]);  
    max.setFullYear(e[0],e[1]);   
    var curr = min;  
    while(curr <= max){  
        var month = curr.getMonth();  
        console.log(month); 
        var str= (month == 0 ? curr.getFullYear() -1 : curr.getFullYear()) +"-"+( month == 0 ? 12 : (month < 10 ) ? '0'+ month : month);
        var s=curr.getFullYear()+"-12";
        if(str==s){
            str=curr.getFullYear()+"-12";
        }
        result.push(str);  
        curr.setMonth(month+1);
    }  
    return result;  
 } 
console.log(getMonthBetween('2016-07', '2018-02'));

//获取两个时间段之间的所有时间

 getalldays(start, end) {
      var startTime = new Date(start).getTime();
      var endTime = new Date(end).getTime();
      for (var k = startTime; k <= endTime; ) {
        console.log(new Date(parseInt(k)));
        k = k + 24 * 60 * 60 * 1000;
      }
    },
 

你可能感兴趣的:(javascript,前端,开发语言)