计算某日期N天前(后)的日期
作者:互联网
// 某日期向前/向后num天 jumpNumDay(date,num) { return new Date(date.getTime()+(num*24*60*60*1000)) }, // 转换日期 changeDate(date) { return date.getFullYear()+'-'+this.changeNum(date.getMonth()+1)+'-'+this.changeNum(date.getDate()) }, // 转换数字 changeNum(num) { if(num >= 10) { return num }else { return '0'+num } }, // 改变日期格式 formatDate(date) { return `${date.getFullYear()}-${this.changeNum(date.getMonth() + 1)}-${this.changeNum(date.getDate())}`; },
调用jumpNumDay()方法,传入日期和天数,向前num传负
console.log(this.formatDate(this.jumpNumDay(new Date('2021-02-26')),-2)) // 2021-02-24 console.log(this.formatDate(this.jumpNumDay(new Date('2021-02-26')),2)) // 2021-02-28
标签:02,return,jumpNumDay,changeNum,日期,num,天前,计算,date 来源: https://www.cnblogs.com/pyx-blog/p/14812598.html