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

241
Views
¿Por qué mi código no compara correctamente las fechas?

Me gustaría comprobar si la fecha está dentro del rango. Creo dos funciones, la primera función transforma el tipo str en Fecha, la segunda función debe encontrar el resultado.

 let probeArray = [{price: 123, date: '2021-11-27'}, {price: 13, date: '2021-11-15'}, {price: 1, date: '2021-10-2'}, {price: 17, date: '2021-10-1'}]; let startDate = '2021-10-1'; let endDate = '2021-10-20'; // transform str to Date const toDate = (dateStr) => { const [year,month,day] = dateStr.split("-"); // console.log('check date') // console.log([day, month, year]) return new Date(year, month - 1, +day+1); } function get_staticstic(probeAr, start, end){ let result = null; let maxDate = toDate(start); let minDate = toDate(end); for (let tempEvent of probeAr){ let currentDate = toDate(tempEvent.date); console.log('maxDate', maxDate); console.log('minDate', minDate); console.log('currentDate',currentDate); if (currentDate >= minDate && currentDate <= maxDate ){ console.log('Correct Date'); } else{ console.log('Out Side range!!'); } } return result } get_staticstic(probeArray, startDate, endDate);

Pero después de que el resultado de inicio para todas las fechas sea '¡¡Rango fuera del lado!!'.

resultado_out

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

0

Problema con el código

  • Error en la función toDate . La definición debe ser return new Date(year, +month - 1, day); . No es necesario agregar 1 con fecha. Además, no es obligatorio que el año y el día sean números, también pueden ser cadenas.
  • Error con minDate y maxDate dentro get_staticstic .

violín de trabajo

 let probeArray = [ { price: 123, date: '2021-11-27' }, { price: 13, date: '2021-11-15' }, { price: 1, date: '2021-10-2' }, { price: 17, date: '2021-10-1' } ]; let startDate = '2021-10-1'; let endDate = '2021-10-20'; // transform str to Date const toDate = (dateStr) => { const [year, month, day] = dateStr.split("-"); // console.log('check date') // console.log([day, month, year]) return new Date(year, +month - 1, day); } function get_staticstic(probeAr, start, end) { let result = null; let minDate = toDate(start); let maxDate = toDate(end); console.log('maxDate', maxDate); console.log('minDate', minDate); for (let tempEvent of probeAr) { let currentDate = toDate(tempEvent.date); console.log('currentDate', currentDate); if (currentDate >= minDate && currentDate <= maxDate) { console.log('Correct Date'); } else { console.log('Out Side range!!'); } } return result } get_staticstic(probeArray, startDate, endDate);

Mejor enfoque

Dado que todas las cadenas de fecha están en formato estándar, no necesita escribir una función de analizador para la fecha. Puede convertir directamente a un objeto de fecha usando el new Date(dateString) .

violín de trabajo

 let probeArray = [ { price: 123, date: '2021-11-27' }, { price: 13, date: '2021-11-15' }, { price: 1, date: '2021-10-2' }, { price: 17, date: '2021-10-1' } ]; let startDate = '2021-10-1'; let endDate = '2021-10-20'; function get_staticstic(probeAr, start, end) { let result = null; let minDate = new Date(start); let maxDate = new Date(end); console.log('maxDate', maxDate); console.log('minDate', minDate); for (let tempEvent of probeAr) { let currentDate = new Date(tempEvent.date); console.log('currentDate', currentDate); if (currentDate >= minDate && currentDate <= maxDate) { console.log('Correct Date'); } else { console.log('Out Side range!!'); } } return result } get_staticstic(probeArray, startDate, endDate);

about 4 years ago · Juan Pablo Isaza Report

0

Debe establecer minDate en la fecha de inicio y maxDate en la fecha de finalización. Hiciste lo contrario.

 let probeArray = [{price: 123, date: '2021-11-27'}, {price: 13, date: '2021-11-15'}, {price: 1, date: '2021-10-2'}, {price: 17, date: '2021-10-1'}]; let startDate = '2021-10-1'; let endDate = '2021-10-20'; // transform str to Date const toDate = (dateStr) => { const [year,month,day] = dateStr.split("-"); // console.log('check date') // console.log([day, month, year]) return new Date(year, month - 1, +day+1); } function get_statistic(probeAr, start, end){ let result = null; let minDate = toDate(start); let maxDate = toDate(end); for (let tempEvent of probeAr){ let currentDate = toDate(tempEvent.date); console.log('maxDate', maxDate); console.log('minDate', minDate); console.log('currentDate',currentDate); if (currentDate >= minDate && currentDate <= maxDate ){ console.log('Correct Date'); } else{ console.log('Out Side range!!'); } } return result } get_statistic(probeArray, startDate, endDate);

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!