JS获取指定日期之前/之后XX天的日期

  /**
   * 获取日期,
   * day,正数向前推X天,负数向后推X天
   */
  function getTime(day) {
    var lw = new Date(new Date() - 1000 * 60 * 60 * 24 * day);//最后一个数字30可改,30天的意思
    var lastY = lw.getFullYear();
    var lastM = lw.getMonth() + 1;
    var lastD = lw.getDate();
    var startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD);//三十天之前日期
    return startdate
  }
  //例子
  console.log(getTime(30)) 
  console.log(getTime(20)) 
  console.log(getTime(-3)) 
  console.log(getTime(32)) 

参考转载:

https://blog.csdn.net/qq_35250826/article/details/87840571

posted @ 2021-03-31 14:15  洛晨随风  阅读(601)  评论(0编辑  收藏  举报