0
我使用的時刻JS來獲得之日起五年日內到未來這段代碼獲得天的數組5天到未來失敗
//current date
var cd = moment().format("DD-MM-YYYY");
//5 days into the future
var nd = moment(cd, "DD-MM-YYYY").add(5, 'days').format('DD-MM-YYYY');
//get all dates from today to 5 days into the future
,現在我試圖讓天之間的陣列current date
和the future date
這是五天後
//current date
var cd = moment().format("DD-MM-YYYY");
//5 days into the future
var nd = moment(cd, "DD-MM-YYYY").add(5, 'days').format('DD-MM-YYYY');
//get all dates from today to 5 days into the future
console.log("start",cd);
console.log("end",nd);
var getDates = function(startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
// Usage
var dates = getDates(cd, nd);
dates.forEach(function(date) {
console.log(date);
});
此爲演示https://jsfiddle.net/codebreaker87/z9d5Lusv/67/
該代碼僅生成當前日期。我怎樣才能生成所有日期之間的數組?