1 //获取最近7天日期 2 3 getDay(0);//当天日期 4 5 getDay(-7);//7天前日期 6 7 8 9 //获取最近3天日期 10 11 getDay(0);//当天日期 12 13 getDay(-3);//3天前日期 14 15 16 17 function getDay(day){ 18 19 var today = new Date(); 20 21 22 23 var targetday_milliseconds=today.getTime() + 1000*60*60*24*day; 24 25 26 27 today.setTime(targetday_milliseconds); //注意,这行是关键代码 28 29 30 31 var tYear = today.getFullYear(); 32 33 var tMonth = today.getMonth(); 34 35 var tDate = today.getDate(); 36 37 tMonth = doHandleMonth(tMonth + 1); 38 39 tDate = doHandleMonth(tDate); 40 41 return tYear+"-"+tMonth+"-"+tDate; 42 43 } 44 45 function doHandleMonth(month){ 46 47 var m = month; 48 49 if(month.toString().length == 1){ 50 51 m = "0" + month; 52 53 } 54 55 return m; 56 57 }