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

135
Views
Filtrado de datos JSON en función de un marco de tiempo para devolver datos que se encuentran entre el intervalo semanal

Tengo un conjunto de datos JSON donde Date() es uno de los parámetros, quiero filtrar estos datos para devolver solo los datos JSON que se encuentran entre una semana y dos semanas.

Tengo una función que convierte mi fecha en días, así que pasé esta función al filtrado de mis datos.

Función de conversión de fecha:

 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; }

Aquí es donde estoy comprobando un marco de tiempo válido contra mi filtro:

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

Estoy filtrando los datos reales con la función anterior, aunque no obtengo el resultado esperado:

 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] }`) ) })

Cómo se ven mis datos predeterminados

 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 answers
Answer question

0

si "2021-10-25 - 2021-11-14 es su entrada,

d.delivery_time_frame.split('-')[0] es "2021" para todas las fechas, por lo que tanto la new Date(startDate) como la new Date(timeEnd) evalúan como 2021-01-01... debe analizar sus fechas de manera diferente, por ejemplo, puede usar subcadenas de longitud fija:

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

EDITAR

una solución probablemente más elegante y robusta sería usar una expresión regular para analizar las fechas:

 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 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!