Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda