1

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

2
  • 2
    Your min date is after your max date. Commented Nov 29, 2021 at 10:26
  • Looks like you have minDate and maxDate mixed up in get_statistics Commented Nov 29, 2021 at 10:27

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.