js当前时间前后N天前后日期的方法

	// 当前时间前后N天前后日期的方法
			function GetWantsDateStr(AddDayCount) {
				let  dd = new Date();
				dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
				let y = dd.getFullYear();
				let m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期,不足10补0
				let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号,不足10补0
				return `${y}${m}${d}`;
			}
			console.log("半年前:" + GetWantsDateStr(-180));
			console.log("三月前:" + GetWantsDateStr(-90));
			console.log("一月前:" + GetWantsDateStr(-30));
			console.log("昨天:" + GetWantsDateStr(-1));
			console.log("今天:" + GetWantsDateStr(0));
			console.log("明天:" + GetWantsDateStr(1));
			console.log("后天:" + GetWantsDateStr(2));
			console.log("一月后:" + GetWantsDateStr(30));
			console.log("三月后:" + GetWantsDateStr(90));
			console.log("半年后:" + GetWantsDateStr(180));
			console.log("五天前:" + GetWantsDateStr(-5));

 

你可能感兴趣的:(JavaScript)