jq封装日期

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() ;
      s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day) ;
      return s ;
    }

使用示例:

获取当前日期(年月日)

getBeforeDate(0)

获取前一天日期(年月日)

getBeforeDate(-1) 

 获取明天日期(年月日)

getBeforeDate(1) 

调用结果展示:

方法的参数值,表示在当前日期的基础上要调整的天数,正数往后加,负数往前推。

猜你喜欢

转载自blog.csdn.net/liangmengbk/article/details/121669345