Spartak Borisov
Spartak Borisov

Reputation: 517

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

Upvotes: 0

Views: 56

Answers (2)

Nitheesh
Nitheesh

Reputation: 19986

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

Upvotes: 2

thchp
thchp

Reputation: 2376

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

Upvotes: 0

Related Questions