<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><script>console.log(getDay(-7)); //一周前-05-21console.log(getDay(-1)); //昨天-05-27console.log(getDay(0)); //今天-05-28console.log(getDay(1)); //明天-05-29console.log(getDay(7)); //一周后-06-04function getDay(day) {//Date()返回当日的日期和时间。var days = new Date();//getTime()返回 1970 年 1 月 1 日至今的毫秒数。var gettimes = days.getTime() + 1000 * 60 * 60 * 24 * day;//setTime()以毫秒设置 Date 对象。days.setTime(gettimes); var year = days.getFullYear();var month = days.getMonth()+1;if(month<10){month="0"+month;}var today = days.getDate();if(today<10){today="0"+today;}return year + "-" + month + "-" + today;}</script></body></html>
如果觉得《js获取一周前 昨天 今天 明天 一周后》对你有帮助,请点赞、收藏,并留下你的观点哦!