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

138
Vistas
Filtering JSON Data based on a time frame to return data that falls between weekly interval

I have a set of JSON data where Date() as one of the parameters, I want to filter this data to return only the JSON data that falls between a week and two weeks.

I have a function that convert my date to days, so I was passing this function into filtering of my data.

Date conversion function:

const dateConverter = (startDate, timeEnd) => {
    const newStartDate = new Date(startDate);
    const newEndDate = new Date(timeEnd);
    const one_day = 1000*60*60*24;
    let result;
    result = Math.ceil((newEndDate.getTime() - newStartDate.getTime()) / (one_day));
    console.log('date Converter result', result);
    if (result < 0) { return 0; }
    return result;
  }

Here is where I am checking for a valid time frame against my filter:

const above_10 = Defaultdata.filter((d, i) => {
    return(d.delivery_time_frame.length > 10);
});

I am filtering the actual data with the function above though I am not getting the expected result:

const next_level = above_10.filter((d, i)=>{
    return(
      // d.delivery_time_frame.split('-')[0] = "2021-10-25"
      dateConverter(`${ d.delivery_time_frame.split('-')[0] }`, `${ d.delivery_time_frame.split('-')[0] }`)
    )
})

What my Default data look like

const Defaultdata = [{date_listed: "4 hours ago",
id: "7857699961",
delivery_time_frame: "2021-10-25 - 2021-11-14",
distance: "22.8 km",
time_stamp: "2021-10-25 16:36:54",
watched: "yes",
} , {
    
date_listed: "3 days ago",
id: "8358962006",
delivery_time_frame: "2021-10-18 - 2021-10-24",
distance: "4.3 km",

time_stamp: "2021-10-22 16:54:12",

}, {
    date_listed: "4 hours ago",
delivery_id: "8146462294",
delivery_time_frame: "2021-10-25",
distance: "4.3 km",
time_stamp: "2021-10-25 16:12:32",

} ]

Basically, what I want to archive from this is to return the data that falls within a week and two weeks based on the timeframe in the data. 

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

if "2021-10-25 - 2021-11-14 is your input,

d.delivery_time_frame.split('-')[0] is "2021" for all dates, so both new Date(startDate) and new Date(timeEnd) evaluate to 2021-01-01... you have to parse your dates differently, for example you can use fixed length substrings:

dateConverter(`${ d.delivery_time_frame.substr(0,10) }`, `${ d.delivery_time_frame.substr(13,10) }`) > 10

EDIT

a probably more elegant and more robust solution would be to use a regular expression to parse for your dates:

let dates = d.delivery_time_frame.match(/(\d\d\d\d-\d\d-\d\d)/g)
dateConverter(`${ dates[0] }`, `${ dates[1] }`) > 10
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