获取本周开始时间,获取本月结束时间,获取本季度开始时间,本年度时间

140 阅读1分钟

获取本周,本月,本季度,本年的开始与结束时间:

export function getThatDate(tabsVal?: string) {
    // const date = dayjs("2025-10-6");
    const date = dayjs();
    let starDate = date.startOf('week');
    let endDate = date.endOf("week");
    switch (tabsVal) {
        case '0':                               //本周
            starDate = date.startOf('week');
            endDate = date.endOf("week");
            break;
        case '1':                               //本月
            starDate = date.startOf('month');
            endDate = date.endOf("month");
            break;
        case '2':                               //本季度
            let month = Math.floor((date.month()) / 3) * 3;
            // console.log('month', month);
            starDate = date.month(month).startOf('month');
            endDate = date.month(month).add(2, "month").endOf('month');
            break;
        case '3':                               //本年
            starDate = date.startOf('year');
            endDate = date.endOf("year");
            break;
    }
    return {
        starDate,
        endDate
    }
}

获取下一周,下一月,下一季度,下一年的开始与结束时间

export function next(tabsVal: string, date: Dayjs) {

    let starDate = date;
    let endDate = date;
    switch (tabsVal) {
        case '0':                              //本周.next
            date = date.add(1, "week");
            starDate = date.startOf('week');
            endDate = date.endOf("week");
            break;
        case '1':                               //本月.next
            date = date.add(1, "month");
            starDate = date.startOf('month');
            endDate = date.endOf("month");
            break;
        case '2':                              //本季度.next
            date = date.add(3, "month");
            starDate = date.startOf('month');
            endDate = date.add(2, "month").endOf('month');
            break;
        case '3':                              //本年.next
            date = date.add(1, "year");
            starDate = date.startOf('year');
            endDate = date.endOf("year");
            break;
    }
    return {
        starDate,
        endDate
    }
}

获取上一周,上一月,上一季度,上一年的开始与结束时间

export function prev(tabsVal: string, date: Dayjs) {
    let starDate = date.startOf('week');
    let endDate = date.endOf("week");
    switch (tabsVal) {
        case '0':
            date = date.add(-1, "week");
            starDate = date.startOf('week');
            endDate = date.endOf("week");
            break;
        case '1':
            date = date.add(-1, "month");
            starDate = date.startOf('month');
            endDate = date.endOf("month");
            break;
        case '2':
            date = date.add(-3, "month");
            starDate = date.startOf('month');
            endDate = date.add(2, "month").endOf('month');
            break;
        case '3':
            date = date.add(-1, "year");
            starDate = date.startOf('year');
            endDate = date.endOf("year");
            break;
    }
    return {
        starDate,
        endDate
    }
}