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

246
Vistas
Why my code don't compare correctly dates?

I would like to check does the date fall within the range. I create two functions, first func transforms type str to Date,second func must find result.

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

But after start result for all dates is 'Out Side range!!'.

out_result

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

0

Issue with code

  • Error in toDate function. Defition should be return new Date(year, +month - 1, day);. No need to add 1 with date. Also its not mandatory for the year and day to be number, they can be string aswell.
  • Error with minDate and maxDate inside get_staticstic.

Working Fiddle

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

Better Approach

Since all date strings ae in standard format, you dont need to write a parser function for date. You can directly convert to date object using new Date(dateString) method.

Working Fiddle

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 Denunciar

0

You should set minDate to the start date and maxDate to the end date. You did the opposite.

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