JS------获取一个时间区间的所有天



1:获取一个时间区间的所有日期

function getDiffDate(start, end) {

    var startTime = getDate(start);

    var endTime = getDate(end);

    var dateArr = [];

    while ((endTime.getTime() - startTime.getTime()) > 0) {

        var year = startTime.getFullYear();

        var month = startTime.getMonth().toString().length === 1 ? "0" + (parseInt(startTime.getMonth().toString(),10) + 1) : (startTime.getMonth() + 1);

        var day = startTime.getDate().toString().length === 1 ? "0" + startTime.getDate() : startTime.getDate();

        dateArr.push(year + "-" + month + "-" + day);

        startTime.setDate(startTime.getDate() + 1);

    }

    return dateArr;

}

 

function getDate (datestr) {

    var temp = datestr.split("-");

    if (temp[1] === '01') {

        temp[0] = parseInt(temp[0],10) - 1;

        temp[1] = '12';

    } else {

        temp[1] = parseInt(temp[1],10) - 1;

    }

    //new Date()的月份入参实际都是当前值-1

    var date = new Date(temp[0], temp[1], temp[2]);

    return date;

}

console.info(getDiffDate('2018-05-27', '2018-06-31'));

 2:获取一个时间区间的所有天

var start_time = '20180201';
var end_time = '20180331';
var bd = new Date(start_time), be = new Date(end_time);
var bd_time = bd.getTime(), be_time = be.getTime(), time_diff = be_time - bd_time;
var d_arr = [];
for (var i = 0; i <= time_diff; i += 86400000)
{
    var ds = new Date(bd_time + i);
    d_arr.push(ds.getDate());
}
console.log(d_arr);

  

 

上一篇:spring默认标签的解析


下一篇:ISO8601格式日期获取