JS 处理时间区间

2018-12-06  本文已影响0人  R_X

获取:本周、上周、下周、本月、上月、下月的时间区间

function getIntager (numb) {
    return parseInt(numb, 10);
}
//  获取当前月份
function getMonthMt () {
    return getIntager(new Date().getMonth()) + 1;
}

function is2length (numb) {
    return numb.toString().length === 2 ? numb : ('0' + numb);
}
//  获取当前年份
function getYearMt () {
    return getIntager(new Date().getFullYear());
}

/**
 * 返回准确的年月
 * @param month
 * @returns {{thisMonth: number, year: number}}
 */
function accurateYAndM (month) {
    let year = 0;
    let thisMonth = 0;
    if (month === 0) {
        year = getYearMt() - 1;
        thisMonth = 12;
    } else if (month === 13) {
        year = getYearMt() + 1;
        thisMonth = 1;
    } else {
        year = getYearMt();
        thisMonth = month;
    }
    return {
        thisMonth: thisMonth,
        year: year
    };
}

//  获取当前X月初日期
function getNowMonthFrist (month) {
    const yAndM = accurateYAndM(month);
    return new Date(yAndM.year + '-' + is2length(yAndM.thisMonth) + '-01');
}
//  获取当前X月末日期
function getNowMonthEnd (month) {
    const yAndM = accurateYAndM(month);
    return new Date(yAndM.year + '-' + is2length(yAndM.thisMonth) + '-01');
}


/**
 * 返回日期
 * @param arg:目标日期相对于今天的步进值
 * @returns {number}
 */
function getDate (arg) {
    const oneDay = 24 * 60 * 60 * 1000;
    const beforeAndAfer = arg * oneDay;
    let atLast = new Date().getTime() + beforeAndAfer;
    if (arguments[1]) {
        atLast = arguments[1].getTime() + beforeAndAfer;
    }
    return new Date(atLast);
}
/**
 * 1、本周
 * 2、下周
 * 3、上周
 * 4、本月
 * 5、上月
 * 6、下月
 * @param {*} arg
 */
export const getDateInterval = function (arg) {
    //  今天周几
    const isNow = new Date().getDay();
    //  通过今天计算出本周一对应的日期
    let startDate = getDate(1 - getIntager(isNow));
    //  通过今天计算出本周天对应的日期
    let endDate = getDate(7 - isNow);
    let diff = 0;
    switch (arg) {
        case 1: //  本周
            startDate = getDate(1 - getIntager(isNow));
            endDate = getDate(7 - isNow);
            break;
        case 2: //  下周
          //  计算出周天离今天还有几天
            diff = 7 - getIntager(isNow);
            //  计算出下周一日期
            startDate = getDate(diff + 1);
            //  计算出下周日日期
            endDate = getDate(7 + diff);
            break;
        case 3: //  上周
          //  计算出上周周天离今天还有几天
            diff = getIntager(isNow);
            //  计算出上周一日期
            startDate = getDate(-(6 + diff));
            //  计算出上周日日期
            endDate = getDate(-diff);
            break;
        case 4: //  本月
          /**
           * ***********************************************
           * 月末计算规则:
           * 获取当前月份然后得到下个月1号对应的时间戳减去一天的时间戳
           *************************************************/
            diff = getMonthMt();
            //  本月初
            startDate = getNowMonthFrist(diff);
            //  本月末
            endDate = getDate(-1, getNowMonthEnd(diff + 1));
            break;
        case 5: //  上月
            diff = getMonthMt();
            //  上月初
            startDate = getNowMonthFrist(diff - 1);
            //  上月月末
            endDate = getDate(-1, getNowMonthEnd(diff), -1);
            break;
        case 6: //  下月
            diff = getMonthMt();
            //  下月初
            startDate = getNowMonthFrist(diff + 1, 1);
            //  下月月末
            endDate = getDate(-1, getNowMonthEnd(diff + 2), 1);
            break;
        default:
            break;
    }
    return {
        startDate,
        endDate
    };
};

上一篇下一篇

猜你喜欢

热点阅读