best way to compare dates

Hey!

I need some advice how to compare dates.

I have 3 selectors to choose year, month, day. They can be set or they can be empty. If empty all data is shown.

  selectedYear: number = 0;
  selectedMonth: number = 0;
  selectedDay: number = 0;

I have an array of objects with dates of type Date. From Date I can extract year, month, day.

For simplicity let’s say array is as follows

datesArray: Array<Date> = [new Date(), new Date(), new Date()]

Now I need a function that will iterate through that array and return true/false if date matches selected criteria.

The problem for me is that selectors can be 0/empty which means I can’t simply compare 2 dates. If only year is selected I have to compare year, if month is selected compare year + month and if day is selected compare full date.

I can make it with a bunch of if statements but I want to make it nice and efficient.

Any advice is appreciated

datesArray.forEach( (date: Date) => {
      if (checkDates(date) == true){
          ...
      }
});
checkDates(date: Date): boolean {
    // Need help here
}

This is a very generic question, probably not best suited for the Ionic forums :upside_down_face:

But, you could concatenate selectedYear, selectedMonth and selectedDay using the yyyyMMdd format into lets say the variable selectedDate. Only concatenate the subsequent values if not empty or zero.

Then create the same format yyyyMMdd for each date in your datesArray.

Now just say if the date in the datesArray starts with selectedDate, it is true.