Assume there is the following array
const array = [
"2023-03-14T13:00:00.000Z",
"2023-03-13T12:30:00.000Z",
"2023-03-06T13:00:00.000Z",
"2023-04-24T11:30:00.000Z",
"2023-03-24T13:00:00.000Z",
]
and a date value
Thu Mar 24 2023 00:00:00 GMT+0100
As you can see, the date value is a start of the day value and in the array there are some specific times.
Now I need to check if there are values in the array existing from the same day. In the example the last date is a match, which should be returned (if possible return strings of hour and minutes only: ['13:00']
)
So first of all I need to get the correct string of the date. I am using date-fns
and I would think I have to use formatISO
, but this gives me '2023-03-24T00:00:00+01:00'
, which I cannot match to the values in the array (indexOf
)
But beside of this, how do I search for values of the same date, but with different time?
2
Answers
You can try converting the dates and date values first and then find similar day values using the
getDate()
method.Then parse the hours and minutes using the date functions.
Working demo-
Since source data structure is array of strings representing datetimes, and you are basically interested in times of days, I’d avoid any conversions to date object whatsoever and handled is in "stringy" way.
One possible way could be to converting it to structure where day strings were keys and array of times would be values:
This will produce
What you could easily query further.