Filtering array of dates by month and year

I have an array of dates in year/month/day format that I want to go through and count the days in each month occuring throughout the array.

I have used the javascript filter method successfully in othe situations but when it comes to dates I found it quite difficult to do.

setDaysMonth(lists.filter((list) => list.date === date range).length)

How do I set the date range for the date coming in from list.date?

Well, if the date is consistently in that format, why not split it on the slashes?


const [year,month,date] = list.date.split("/")

At that point, you’re simply filtering those month values that meet your criteria.

Hi and welcome to the forums. We don’t have enough information or context to be able to offer any specific help.
What code have you written so far? In what way is it not working? What does your data structure look like? Are you trying to add information to that data structure or are you trying to find data members?

That worked thank you.
I put the logic in to a function to return a boolean

setDaysMonth(lists.filter((list) => inDateRange(‘01’,list.date)).length)

const inDateRange = (range,thedate)=>{
let [year,month,date] = thedate.split("-")
return month===range? true : false
}

thanks again SnowMonkey

1 Like