This problem is quite easy but I still can't handle it. I have an array of object let's say something like:
const array = [{id:1,created_at: 2022-03-14T16:40:10.000000Z}, {id:2, created_at:2022-03-15T16:40:10.000000Z}
I want to filter element using moment.js, and having back only the element that have date of creation of today.
What i want to achieved is something like this:
const filtered = array.filter((item)=> item.created_at >= startOfToday && item.created_at <= endOfToday
Filtered will look like: [0]{id:2, created_at:2022-03-15T16:40:10.000000Z}
Thanks in advance for any tips/answers.
CodePudding user response:
Can you use statOf
and endOf
to make your filter work?
const filtered = array.filter(item => {
const createdAt = moment(item.created_at)
const startOfToday = moment().startOf('day')
const endOfToday = moment().endOf('day')
return createdAt.isBetween(startOfToday, endOfToday)
})