JS 获取最近(前)7天(一周内)和最近(前)3天日期(转)

 //当天日期
 getDay(0);

 //7天前日期
 getDay(-7);
  
 //3天前日期
 getDay(-3);
  
  
 function 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 = doHandleMonth(tMonth + 1);
   tDate = doHandleMonth(tDate);
   return tYear+"-"+tMonth+"-"+tDate;
 }
  
 function doHandleMonth(month){
   var m = month;
   if(month.toString().length == 1){
     m = "0" + month;
   }
   return m;
 }

你可能感兴趣的:(JS 获取最近(前)7天(一周内)和最近(前)3天日期(转))