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 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();
var unixDe = de.getTime();
for (var k = unixDb; k <= unixDe;) {
console.log((new Date(parseInt(k))).format());
k = k + 24 * 60 * 60 * 1000;
}
}
getAll('2018-05-27', '2018-04-20');
js獲取兩個日期之間所有日期
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.