skip to Main Content

I have an array of dates.

['2023-06-02T00:00:00.000+03:00','2023-06-02T00:00:00.000+03:00','2023-12-04T00:00:00.000+03:00']

I need that when switching in the date component, I compare the selected date with the given array and give the nearest date if I switched to the right or left
I started making a method. But I don’t understand how to compare this in a cycle

to the left – the date is less, to the right – the date is more

 const dateSwitch = (prev: boolean) => {
    let newDate: string = DateTime.fromJSDate(new Date()).startOf("day")
    if (enabledDates.value.length) {
      if (prev) enabledDates.value.forEach((enableDateFromArr,idx) => {
        const enableDate = DateTime.fromISO(enableDateFromArr).startOf("day").toISO()
        const compare = enableDate >= date.value
      }) 
    } 
    }
  }

3

Answers


  1. Convert the selected date and the dates in the array using the DateTime.fromISO(). Then, iterate over the array of dates and calculate the difference between each date and the selected date. Using reduce(), find the index of the date with the smallest difference and return the date at the index with the smallest difference.

    const dateSwitch = (prev: boolean) => {
     let newDate: string = DateTime.fromJSDate(new Date()).startOf("day").toISO();
     if (enabledDates.value.length) {
       let selectedDate = DateTime.fromISO(newDate);
       let dates = enabledDates.value.map(date => DateTime.fromISO(date));
       let differences = dates.map(date => selectedDate.diff(date, 'days').days);
       let minIndex = differences.reduce((minIndex, diff, index) => diff < differences[minIndex] ? index : minIndex, 0);
       return dates[minIndex].toISO();
     }
    }
    
    Login or Signup to reply.
  2. Here’s a function in JavaScript that takes an array of ISO date strings and a selected date, and returns the nearest date to the selected date :

    function getNearestDate(dates, selectedDate) {
      let nearestDate = dates[0];
      let nearestDiff = Math.abs(new Date(dates[0]).getTime() - new Date(selectedDate).getTime());
    
      for (let i = 1; i < dates.length; i++) {
        const diff = Math.abs(new Date(dates[i]).getTime() - new Date(selectedDate).getTime());
        if (diff < nearestDiff) {
          nearestDiff = diff;
          nearestDate = dates[i];
        }
      }
    
      return nearestDate;
    }
    
    const dates = ['2023-12-22T00:00:00Z', '2024-01-29T00:00:00Z', '2024-02-19T00:00:00Z'];
    const selectedDate = '2024-01-01T00:00:00Z';
    const nearestDate = getNearestDate(dates, selectedDate);
    console.log(nearestDate); // Outputs: '2023-12-22T00:00:00Z'
    Login or Signup to reply.
  3. const allDates = ['2023-06-02T00:00:00.000+03:00','2023-06-02T00:00:00.000+03:00','2023-12-04T00:00:00.000+03:00']
    const searchDate = '2023-10-03T00:00:00.000+03:00'
    const tsDates = allDates.map((e) => {
        return { "ts":Date.parse(e), "date": e, "diff": Math.abs(Date.parse(e) - Date.parse(searchDate))}
    }).sort((a, b) => a.diff - b.diff)
    
    console.log(tsDates)
    //Nearest will be the first one in the Array
    console.log(`Nearest date of ${searchDate} is: ${tsDates[0].date}`)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search