JS设置前后7、30天(前n天,当前,后n天)

在项目中,有时候需要查询一段时间以内的订单。为了方便用户快速选择时间区域,并写了这个需求,格式

为:YY-MM-DD hh:mm:ss

function getBeforeDate(n){//n为你要传入的参数,当前为0,前一天为-1,后一天为1 
      var date = new Date() ;
      var year,month,day ;
      date.setDate(date.getDate()+n);
      year = date.getFullYear();
      month = date.getMonth()+1;
      day = date.getDate();
    var strHours = date.getHours();
    var strMinutes = date.getMinutes();
    var strSeconds = date.getSeconds();
      s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day)+' '+ ( strHours <10 ? ('0'+strHours ) : strHours)+':'+(strMinutes < 10 ? ('0'+strMinutes) : strMinutes)+':'+(strSeconds < 10 ? ('0'+strSeconds) : strSeconds);
      return s

 }

猜你喜欢

转载自blog.csdn.net/weixin_41760500/article/details/82964023