js获取指定日期之前/之后的某天——tool


var date_getPointDate = function (currDate, num) {  //num表示天数,接受正负数
  if(!num){//做num简单验证
    return currDate;
  }
  num = Math.floor(num);
  var symbol = '/';
  if(currDate.indexOf('-')>-1){
    symbol = '-';
    currDate = currDate.replace(/-/g,'/');
  }else if(currDate.indexOf('.') > -1){
    symbol = '.';
    currDate = currDate.replace(/\./g,'/');
  }
  //symbol = '-'; //定制输出分隔符
  var myDate = new Date(currDate),
      lw = new Date(Number(myDate) + 1000 * 60 * 60 * 24 * num), //num天数
      lastY = lw.getFullYear(),
      lastM = lw.getMonth()+1,
      lastD = lw.getDate(),
      startdate=lastY+ symbol +(lastM<10 ? "0" + lastM : lastM)+ symbol +(lastD<10 ? "0"+ lastD : lastD);
  return startdate;
}

console.log(date_getPointDate("2017.10.24", 30))  //2017-11-23
console.log(date_getPointDate("2017-10-24", -30)) //2017/09/24
/* 
 * 调用规则 :
 * 指定日期n天前日期:date_getPointDate("2017/11/26", -30)
 * 指定日期n天后日期:date_getPointDate("2017/10/27", 30)
 */

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM