js如何獲取前後連續n天的時間
阿新 • • 發佈:2018-11-08
1 function GetDateStr(AddDayCount) { 2 var dd = new Date(); 3 dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期 4 var y = dd.getFullYear(); 5 var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//獲取當前月份的日期,不足10補0 6 var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//獲取當前幾號,不足10補0 7 return y+"-"+m+"-"+d; 8 } 9 10 11 console.log("半年前:"+GetDateStr(-180)); 12 console.log("三月前:"+GetDateStr(-90)); 13 console.log("一月前:"+GetDateStr(-30)); 14 console.log("昨天:"+GetDateStr(-1)); 15 console.log("今天:"+GetDateStr(0)); 16 console.log("明天:"+GetDateStr(1)); 17 console.log("後天:"+GetDateStr(2));18 console.log("一月後:"+GetDateStr(30)); 19 console.log("三月後:"+GetDateStr(90)); 20 console.log("半年後:"+GetDateStr(180));