skip to Main Content

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-fnsand 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


  1. 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-

    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",
    ]
    
    // convert date value
    let dv = "Thu Mar 24 2023 00:00:00 GMT+0100"
    dv = new Date(dv).getDate();
    
    // find the dates which matches with date value's date
    let result = array.filter(item => new Date(item).getDate() == dv)
    
    // loop on each date and parse the hour and minute
    result = result.map(item => {
      const date = new Date(item);
      const hours = date.getUTCHours().toString();
      const minutes = date.getUTCMinutes().toString();
      return hours.padStart(2, '0') + ':' + minutes.padStart(2, '0');
    })
    
    // result
    console.log(result)
    Login or Signup to reply.
  2. 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.

    Now I need to check if there are values in the array existing from the same day. In the example the last two dates are matches, which should be returned (if possible return strings of hour and minutes only: [’11:30′, ’13:00′])

    One possible way could be to converting it to structure where day strings were keys and array of times would be values:

    const array = [
      "2023-03-06T13:00:00.000Z",
      "2023-04-24T11:30:00.000Z",
      "2023-04-24T13:00:00.000Z", // shifted this one by month to be same day as previous
    ];
    
    const result = array.reduce((acc, e) => {
      let day = e.slice(0, 10);
      let time = e.slice(11, 16);
      acc[day] = acc[day] || [];
      acc[day].push(time);
      return acc
    }, {});
    
    console.log(JSON.stringify(result, null, 't'));

    This will produce

    {
        "2023-03-06": [
            "13:00"
        ],
        "2023-04-24": [
            "11:30",
            "13:00"
        ]
    }
    

    What you could easily query further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search