Skip to content

Instantly share code, notes, and snippets.

@spolat
Last active July 23, 2020 00:51
Show Gist options
  • Save spolat/b08cc16f98398aff86bcc26fad33040d to your computer and use it in GitHub Desktop.
Save spolat/b08cc16f98398aff86bcc26fad33040d to your computer and use it in GitHub Desktop.
Get dates between in two dates javascript
function getDates(startDate, endDate) {
let currentDate = new Date(startDate);
let tomorrow = new Date(currentDate);
tomorrow.setDate(tomorrow.getDate() + 1);
const dates = [];
while (currentDate < endDate) {
dates.push({
startDate: new Date(currentDate),
endDate: new Date(tomorrow)
});
currentDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
tomorrow = new Date(tomorrow.setDate(tomorrow.getDate() + 1));
}
return dates;
}
const dates = getDates(new Date('2020-07-20'), new Date('2020-07-23'));
console.log(dates);
[ { startDate: Mon Jul 20 2020 03:00:00 GMT+0300 (GMT+03:00), 
endDate: Tue Jul 21 2020 03:00:00 GMT+0300 (GMT+03:00) }, 
{ startDate: Tue Jul 21 2020 03:00:00 GMT+0300 (GMT+03:00), 
endDate: Wed Jul 22 2020 03:00:00 GMT+0300 (GMT+03:00) }, 
{ startDate: Wed Jul 22 2020 03:00:00 GMT+0300 (GMT+03:00), 
endDate: Thu Jul 23 2020 03:00:00 GMT+0300 (GMT+03:00) } ] 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment