如何在javascript中循环播放日期范围?

时间:2017-09-19 02:25:00

标签: javascript html web

我得到开始日期(2017-09-14)和结束日期(2017-09-16),并希望循环显示日期范围以获取月份和年份进行比较。我怎么能这样做?

[ ...document.getElementsByClassName("event_date") 
].forEach( el => { 
const result = el.textContent.split("to"); 
const startday = result[0].split("-"); 
const endday = result[1].split("-"); 

if( startday[1] != {{:charmonths[month]}} || startday[2] != {{: year}} )

1 个答案:

答案 0 :(得分:0)

我们可以在当前添加一天,这会导致日期翻到下个月。

let endDate = new Date(); // today
let startDate = new Date(2017, 0, 1); // Jan 1st 2017
let daysOfYear = [];
for (let day = startDate;day <= endDate; day.setDate(day.getDate() + 1)) {
     daysOfYear.push(new Date(day)); 
}

简单。