I am trying to create an array of months that have nested arrays with all the days in that month between a date range in JavaScript with MomentJS.
aiming to achieve: if my date range was May 1st – June 3rd
Using this loop i can successfully build an array of days but i cannot split them into months..
while (currentDate <= endDate) {
currentDate.setDate(currentDate.getDate() + 1);
dates.push(new Date(currentDate));
This is pretty much what i want to achieve:
[May - month name
1st May - date object for day
2nd May
3rd May
4th May
...]
June
[1st June
2nd June
3rd June]
Any help would be appreciated!
2
Answers
You can use plain; vanilla JavaScript without any libraries to accomplish this. The
Intl
library provides an API for display time/date and number formats based on a locale.You could try something like this. A function for collecting the days between two days, and nother for grouping by month (you need year as well then).
https://jsfiddle.net/xpz43rog/1