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