Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How to filter ISO DateTime objects outside the range of specific time interval

I have a list of objects with startTime and endTime properties in ISO format for a project I am currently working on. Here is a sample:

list = [
 {
   startTime: '2022-06-26T10:00:00.000Z',
   endTime: '2022-06-26T10:30:00.000Z'
 },
 {
   startTime: '2022-06-26T10:35:00.000Z',
   endTime: '2022-06-26T11:00:00.000Z'
 },
 {
   startTime: '2022-06-26T11:45:00.000Z',
   endTime: '2022-06-26T12:15:00.000Z'
 },
 {
   startTime: '2022-06-26T12:10:00.000Z',
   endTime: '2022-06-26T12:25:00.000Z'
 },
 {
   startTime: '2022-06-26T12:20:00.000Z',
   endTime: '2022-06-26T12:45:00.000Z'
 },
 {
   startTime: '2022-06-26T12:40:00.000Z',
   endTime: '2022-06-26T13:00:00.000Z'
 }
]

And I have an object assignment which has two properties like the objects of the list array above.

const assignment = {
   startTime: '2022-06-26T12:00:00.000Z',
   endTime: '2022-06-26T12:30:00.000Z'
}

My task is to filter the objects from the list array which do not fall into the interval/overlap with the assignment. For example, I want to include the following in my result array because they do not overlap/inside the interval of the assignment:

const result = [
  {
   startTime: '2022-06-26T10:00:00.000Z',
   endTime: '2022-06-26T10:30:00.000Z'
  },
 {
   startTime: '2022-06-26T10:35:00.000Z',
   endTime: '2022-06-26T11:00:00.000Z'
 },
 {
   startTime: '2022-06-26T12:40:00.000Z',
   endTime: '2022-06-26T13:00:00.000Z'
 }
]

But I want to exclude the following from my result array because they overlap/inside the interval of the assignment object.

[
  {
   startTime: '2022-06-26T11:45:00.000Z',
   endTime: '2022-06-26T12:15:00.000Z'
 },
 {
   startTime: '2022-06-26T12:10:00.000Z',
   endTime: '2022-06-26T12:25:00.000Z'
 },
 {
   startTime: '2022-06-26T12:20:00.000Z',
   endTime: '2022-06-26T12:45:00.000Z'
 },
]

This is my idea so far: So, there are basically 5 scenarios.

Scenario 1: startTime and endTime of a list object are before the startTime of assignment
Scenario 2: startTime of a list object is before the startTime of the assignment but endTime is after startTime and before endTime of the assignment
Scenario 3: startTime and endTime of a list object falls entirely into the interval of the startTime and endTime of the assignment object
Scenario 4: startTime of a list object is after the startTime and before the endTime of assignment but endTime(list object) is after the endTime of the assignment  
Scenario 5: Both startTime and endTime of the list object are after the endTime of the assignment

Other than writing a lot of if-else conditions, what is the best approach to filter the list of objects? I am using Javascript with luxon library. Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can check that the intervals don't overlap by checking that the endTime in list is before the startTime in assignment, or that the startTime in list is after the endTime in assignment:

list = [
 { startTime: '2022-06-26T10:00:00.000Z', endTime: '2022-06-26T10:30:00.000Z' },
 { startTime: '2022-06-26T10:35:00.000Z', endTime: '2022-06-26T11:00:00.000Z' },
 { startTime: '2022-06-26T11:45:00.000Z', endTime: '2022-06-26T12:15:00.000Z' },
 { startTime: '2022-06-26T12:10:00.000Z', endTime: '2022-06-26T12:25:00.000Z' },
 { startTime: '2022-06-26T12:20:00.000Z', endTime: '2022-06-26T12:45:00.000Z' },
 { startTime: '2022-06-26T12:40:00.000Z', endTime: '2022-06-26T13:00:00.000Z' }
]

const assignment = { startTime: '2022-06-26T12:00:00.000Z', endTime: '2022-06-26T12:30:00.000Z' }

const aDates = { 
  startTime: new Date(assignment.startTime),
  endTime: new Date(assignment.endTime)
}

const result = list.filter(({ startTime, endTime }) => 
  new Date(startTime) > aDates.endTime ||
  new Date(endTime) < aDates.startTime
)

console.log(result)
.as-console-wrapper { max-height:100% !important; top: 0 }

Note I've converted the dates in assignment to Date outside the loop to save the conversion happening multiple times.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!