Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda