js获取时间范围内所有日期

1、获取日期范围内天粒度所有天

function getYearAndMonthAndDay(start,end){
        var date_all=[],i=0;
        var startTime = getDate(start);
        var endTime = getDate(end);
        while((endTime.getTime()-startTime.getTime())>=0){
          var year = startTime.getFullYear();
          var month = (startTime.getMonth()+1).toString().length==1?"0"+(startTime.getMonth()+1).toString():(startTime.getMonth()+1).toString();
          var day = startTime.getDate().toString().length==1?"0"+startTime.getDate():startTime.getDate();
          date_all[i]=year+"-"+month+"-"+day;
          startTime.setDate(startTime.getDate()+1);
          i+=1;
        }
        return date_all;
    };

2、获取日期范围内月粒度范围所有月

function getYearAndMonth(start, end) {
        var result = [];
        var starts = start.split('-');
        var ends = end.split('-');
        var staYear = parseInt(starts[0]);
        var staMon = parseInt(starts[1]);
        var endYear = parseInt(ends[0]);
        var endMon = parseInt(ends[1]);
        result.push(staYear+"-"+starts[1]);
        result.push(endYear+"-"+ends[1]);
        while (staYear <= endYear) {
            if (staYear === endYear) {
                while (staMon < endMon) {
                    staMon++;
                    if(staMon.length = 1){
                    	staMon = "0"+staMon;
                    }
                    result.push(staYear+"-"+staMon);
                }
                staYear++;
            } else {
                staMon++;
                if (staMon > 12) {
                    staMon = 1;
                    staYear++;
                }
                if(staMon.length = 1){
                	staMon = "0"+staMon;
                }
                result.push(staYear+"-"+staMon);
            }
        }
        return result;
    }

 

你可能感兴趣的:(js)