VUE获取当前日期的周一和周天

weekCheckingIn(){
    this.weekChecking_inloading = true
    var now = new Date();
    var nowTime = now.getTime() ;
    var day = now.getDay()|| 7;// 不加 || 7.周末会变成下周一
    var oneDayTime = 24*60*60*1000 ;
    var MondayTime = nowTime - (day-1)*oneDayTime ;//显示周一
    var SundayTime =  nowTime + (7-day)*oneDayTime ;//显示周日
    //调用方法
    console.log(this.formatDate(new Date(MondayTime))),
    console.log(this.formatDate(new Date(SundayTime)))
}

//格式化日期:yyyy-MM-dd
formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();
    
    if (mymonth < 10) {
    mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
    myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}
posted @ 2021-06-22 14:35  岁月淡忘了谁  阅读(2031)  评论(0编辑  收藏  举报