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)
 */

 

posted @ 2017-10-28 10:58  游龙翔隼  阅读(1607)  评论(0编辑  收藏  举报