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;},
使用
//一周前getDay(-7); //'-04-08'//一个月前getDay(-30); //'-03-16'//三个月前getDay(-90); //'-01-15'//一周后getDay(7); //'-04-22'//一个月后getDay(30); //'-05-15'
如果觉得《JavaScript获取一周前 一个月前日期》对你有帮助,请点赞、收藏,并留下你的观点哦!