I’m trying to split the working time into working slots while excluding Break time.
We may have multiple working slots and multiple breaks but time could not overlap.
while excluding the break hours and splitting the working time.
To exclude the time slots Exclusion: True means exclude that time from the Exclusion: false object.
example: This is the array of objects with working hours and exclusion hours.
[
{
"startHours": "2023-07-13T01:00:44.000Z",
"finishHours": "2023-07-13T13:00:08.000Z",
"exclusion": false
},
{
"startHours": "2023-07-13T08:00:19.000Z",
"finishHours": "2023-07-13T09:00:24.000Z",
"exclusion": true
},
{
"startHours": "2023-07-13T11:00:11.000Z",
"finishHours": "2023-07-13T11:30:26.000Z",
"exclusion": true
}
]
And The output I want array of objects with updated working hours to display only working time slots.
[
{
"startHours": "2023-07-13T01:00:44.000Z",
"finishHours": "2023-07-13T08:00:19.000Z",
},
{
"startHours": "2023-07-13T09:00:24.000Z",
"finishHours": "2023-07-13T11:00:26.000Z",
},
{
"startHours": "2023-07-13T11:30:26.000Z",
"finishHours": "2023-07-13T13:00:08.000Z",
}
]
Then I am converting the time to display on the table like in the below example. This is for just understanding.
These are the working hours.
06:00-13:00
14:00-16:00
16:30-18:00
2
Answers
Converting strings to Date and vice versa, and comparing dates are all doable with
Date
objects, so I won’t get into that.If we remove that from the problem, it comes down to finding the segments out of this array:
The entire solution can be long but this is roughly how you can do it:
start
andend
values. In the above example, it becomes 1, 6, 15, 35, 50, 100.excluded
flag.I provided explanation with comments. Bear in mind that the results could very depending on your timezone. You could specify a time zone to get the same results everywhere but I didn’t know that was needed.