计算近7天时间(可跨年)

var myDate = new Date(); // 获取今天日期
let Y = myDate.getFullYear();
let M = myDate.getMonth() + 1 - 0 >= 10 ? Number(myDate.getMonth()) + 1 : '0' + (Number(myDate.getMonth()) + 1);
let D = myDate.getDate() >= 10 ? Number(myDate.getDate()) : '0' + (Number(myDate.getDate()));
this.time = Y + '-' + M + '-' + D;var dateTemp
var flag = 1
for (var i = 0; i < 7; i ++) {
    dateTemp = myDate.getFullYear() + '-' + (myDate.getMonth() + 1 - 0 >= 10 ? Number(myDate.getMonth()) + 1 : '0' + (Number(myDate.getMonth()) + 1)) + '-' + (myDate.getDate() >= 10 ? Number(myDate.getDate()) : '0' + (Number(myDate.getDate())));
    this.dateArray.push(dateTemp);
    myDate.setDate(myDate.getDate() + flag)
}

 

posted @ 2022-07-22 14:21  小蘑菇123  阅读(29)  评论(0编辑  收藏  举报