js获取指定时间范围内的连续小时、天、周、月列表

function getDuration(type,start,stop){
    var $array = new Array();
    var current = new Date(start);
    stop  = new Date(stop);
    while (current <= stop) {
        $array.push( new Date (current) );
        if(type == 'hour'){//小时
             current.setHours(current.getHours() + 1);
        }else if(type == 'day'){//天
            current.setDate(current.getDate() + 1);
        }else if(type == 'week'){//周
            current.setDate(current.getDate() + 7);
        }else if(type == 'month'){//月
            current.setMonth(current.getMonth() + 1);
        }else{//默认天
            current.setDate(current.getDate() + 1);
        }
    }
    return $array;
}

console.log(getDuration('day','2019-10-05 10:23:16','2019-11-05 18:23:16'));

你可能感兴趣的:(node.js,javascript)