console.log(this.getDay(+10)); //当前时间后10天的日期
console.log(this.getDay(-10)); //当前时间前10天的日期
getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear + "-" + tMonth + "-" + tDate;
},
doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
},
//获取某年某月某日 是星期几
//定义大写数字数组
var weekArray = new Array('日', '一', '二', '三', '四', '五', '六');
//通过获得下标拿去大写文字
var week = weekArray[new Date(‘yy-mm-dd’).getDay()];
console.log(‘星期’+'week');
查看某年某月某日的一定范围内是哪天
getNextDate(date, day) { //date格式为 yy-mm-dd day一定是数字类型
var dd = new Date(date);
dd.setDate(dd.getDate() + parseInt(day));
var y = dd.getFullYear();
var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return y + "-" + m + "-" + d;
},
//例如我要查询2021-01-02的前一天和后一天
console.log(getNextDate(‘2021-01-02’ , -1)); //前一天
console.log(getNextDate(‘2021-01-02’ , 1)); //后一天