JS 获取本周一,本周日,上周一,上周日,下周一,下周日,本月第一天,本月最后一天,上月第一天,上月最后一天,下月第一天,下月最后一天

Date.prototype.format = function (fmt) {
    const o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}


function previousMonthFirst() {
    // 上月第一天
    let currentDate = new Date();
    currentDate.setMonth(currentDate.getMonth() - 1)
    const y = currentDate.getFullYear(); //获取年份
    let m = currentDate.getMonth() + 1; //获取月份
    m = m < 10 ? "0" + m : m; //月份补
    return [y, m, '01'].join("-");
}

function previousMonthLast() {
    //上月最后一天 获取本月第一天减一天
    const currentDate = new Date();
    const y = currentDate.getFullYear();
    let m = currentDate.getMonth() + 1;
    m = m < 10 ? "0" + m : m; //月份补 0

    const currentDate2 = new Date([y, m, '01'].join("-"))
    currentDate2.setDate(currentDate2.getDate() - 1)
    return currentDate2.format("yyyy-MM-dd")
}


function currentMonthFirst() {
    //当前月第一天
    const currentDate = new Date(); //获取年份
    const y = currentDate.getFullYear();
    let m = currentDate.getMonth() + 1;
    m = m < 10 ? "0" + m : m; //月份补 0
    return [y, m, "01"].join("-");
}

function currentMonthLast() {
    //当前月最后一天 获取下一个月第一天减一天
    const currentDate = new Date()
    currentDate.setMonth(currentDate.getMonth() + 1)

    let y = currentDate.getFullYear()
    let m = currentDate.getMonth() + 1
    m = m < 10 ? "0" + m : m
    const monthLast = new Date([y, m, '01'].join('-'));

    monthLast.setDate(monthLast.getDate() - 1)
    return monthLast.format("yyyy-MM-dd")
}


function nextMonthFirst() {
    //下个月第一天
    const nextFirst = new Date()
    nextFirst.setMonth(nextFirst.getMonth() + 1) //增加一个月
    const nextOne2 = new Date([nextFirst.getFullYear(), nextFirst.getMonth() + 1, '01'].join('-'));
    return nextOne2.format("yyyy-MM-dd")
}

function nextMonthLast() {
    // 下个月最后一天 下两个月第一天 减一天
    const nextLast = new Date()
    nextLast.setMonth(nextLast.getMonth() + 2) //增加两个月

    let y = nextLast.getFullYear()
    let m = nextLast.getMonth() + 1
    m = m < 10 ? "0" + m : m
    const nextLast2 = new Date([y, m, '01'].join('-'));

    nextLast2.setDate(nextLast2.getDate() - 1)
    return nextLast2.format("yyyy-MM-dd")
}


const now = new Date();
const nowTime = now.getTime();
const day = now.getDay();
const oneDayTime = 24 * 60 * 60 * 1000;

//本周的周一
let BMondayTime = nowTime - (day - 1) * oneDayTime;
//本周的周日
let BSundayTime = nowTime + (7 - day) * oneDayTime;

BMondayTime = new Date(BMondayTime).format("yyyy-MM-dd");
BSundayTime = new Date(BSundayTime).format("yyyy-MM-dd");


//上周的周一
let SMondayTime = nowTime - (day + 6) * oneDayTime;
//上周的周日
let SSundayTime = nowTime + (0 - day) * oneDayTime;

SMondayTime = new Date(SMondayTime).format("yyyy-MM-dd");
SSundayTime = new Date(SSundayTime).format("yyyy-MM-dd");


//下周的周一
let AMondayTime = nowTime - (day - 8) * oneDayTime;
//下周的周日
let ASundayTime = nowTime + (8 + day) * oneDayTime;

AMondayTime = new Date(AMondayTime).format("yyyy-MM-dd");
ASundayTime = new Date(ASundayTime).format("yyyy-MM-dd");


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值