js實現獲取兩個日期之間所有日期最簡單的方法


  Date.prototype.format = function() {

     var s = '';
     var mouth = (this.getMonth() + 1)>=10?(this.getMonth() + 1):('0'+(this.getMonth() + 1));
     var day = this.getDate()>=10?this.getDate():('0'+this.getDate());
     s += this.getFullYear() + '-'; // 獲取年份。
     s += mouth + "-"; // 獲取月份。
     s += day; // 獲取日。
     return (s); // 返回日期。
  };

  function getAll(begin, end) {
    var arr = [];
    var ab = begin.split("-");
    var ae = end.split("-");
    var db = new Date();
    db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
    var de = new Date();
    de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
    var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
    var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
    for (var k = unixDb; k <= unixDe;) {
      //console.log((new Date(parseInt(k))).format());
      k = k + 24 * 60 * 60 * 1000;
      arr.push((new Date(parseInt(k))).format());
    }
    return arr;
}

console.log(getAll('2018-07-05','2018-08-05'));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM