(1)、只能获取今天之前的时间
今天:直接new Date();
昨天:今天减去一天(oneday = 1000*60*60*24);
一周:今天减去一天*7;
最后用format格式化
var today = new Date(); var oneday = 1000 * 60 * 60 * 24; var yesterday = new Date(today - oneday); //只能用减法,加法用着没有效果 console.log(yesterday.format('-'))
(2)、获取昨天或是明天等日期
function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth() + 1; //获取当前月份的日期 var d = dd.getDate(); //最后再把月、日的个位数情况处理一下 m = '0' + m; m = m.substr(m.length - 2); d = '0' + d; d = d.substr(d.length - 2); return y + "-" + m + "-" + d; } document.write("前天:" + GetDateStr(-2)); document.write("<br />昨天:" + GetDateStr(-1)); document.write("<br />今天:" + GetDateStr(0)); document.write("<br />明天:" + GetDateStr(1)); document.write("<br />后天:" + GetDateStr(2)); document.write("<br />大后天:" + GetDateStr(3));