function get_date(num){
var date = new Date(); //获取今天的时间
var today = date.getFullYear()+ '-'+ (date.getMonth() +1) + '-' + date.getDate();
console.log(today);
var new_date = new Date(date);
new_date.setDate(date.getDate() + num);
// num为正数时,获取后num天 ,为负数时,获取前num天,0表示今天。
var new_day= new_date.getFullYear() + '-' + (new_date.getMonth()+1) + '-' +new_date.getDate();
console.log(new_day);
return new_day;
}
get_date(7);
//2021-1-4 2021-1-11
get_date(-7);
//2021-1-4 2020-12-28
js获取日期对应周几
function get_weekday(datestr){
var weekArray = new Array ('周日','周一','周二','周三','周四','周五','周六');
var weekday = weekArray[new Date(datestr).getDay()];
console.log(weekday);
return weekday;
}
get_weekday('2021-01-04'); //周一