1

If I had an array of dates, is there a way I could match up another date by rounding up until one is matched?

For example, say I have an array of dates:

"2022-09-15"
"2022-10-10"
"2022-12-01"

And I have a date pulled from the application: "2022-09-29", I want the date to update itself by rounding up until the next upcoming date ("2022-10-10") is selected.

I am unsure how I would round up like I could in mathematics situations.

halfer
  • 19,824
  • 17
  • 99
  • 186
Matthew
  • 1,565
  • 6
  • 32
  • 56

2 Answers2

0

Assuming your dates are in order, you can iterate through your array starting at the beginning until you find the first date that is bigger than you date provided by the application. In JavaScript, your can do a direct comparison like this:

"2022-09-15" > "2022-10-10" // false
"2022-09-15" < "2022-10-10" // true

Note that this works because of the ordering of the year, month, and day that you have presented. If you wanted to do comparisons where you had day, month, year, you would want to create a Date JavaScript object and do the comparisons that way. You can read more about those here: Compare two dates with JavaScript

But for your use case, a simple loop could look like this:

for(let i = 0; i < array.length; i++) {
   if(applicationDate < array[i])
      return array[i]
}
Drew Pesall
  • 179
  • 3
  • 12
  • For this solution to work properly, the order of the dates needs to be in ascending order. – Jesse Sep 15 '22 at 20:10
0

You don't necessarily need to "round" the dates up. Incrementing the date and comparing it to every entry in the array until you find a match would take a relatively large amount of time and resources. I prefer a kind of "knock-out" approach to problems like this. Simply rule out everything it can't be until you're left with a single option. In this case, since you specifically need a date that comes after the input date, we can first rule out anything before the input date. We can then take this new list of dates (that we now know are all after the input date) and get the "smallest" one. This will effectively give you the date that is closest to the input date but still after it.

In your question you presented the dates as a list of strings. This isn't a huge deal because this can still be fairly easily accomplished, but the strings must be in a format that JavaScript recognizes as a date, otherwise all comparisons will result in false. Here is a list of the valid date formats.

I personally like to avoid depending on the order of arrays just because it can be hard to maintain and if/when it breaks, it's generally very hard to find that the issue is that the array is out of order (speaking from experience here). For this reason, the code examples provided here will be completely unreliant on the order of the array.

First, let's discuss a solution using Date objects. This is fairly straight forward. The only thing is that you would need to make sure the date being input is in a valid format as discussed previously. Keep in mind the input needs to be converted to a Date object (if it isn't already) because comparisons between date strings and Date objects always return false. To get only dates after the current date, we can use Array.prototype.filter(), and to get the "smallest" date afterwards we can use Math.min.apply() as explained in this Stack Overflow answer.

var dates = [
    new Date("2022-09-15"),
    new Date("2022-10-10"),
    new Date("2022-12-01")
];

var inputDate = new Date("2022-09-29");

var datesAfter = dates.filter(x => x > inputDate);
var closestDate = new Date(Math.min.apply(null,datesAfter));
console.log(closestDate);

Now for date strings. The idea is largely the same as Date objects. The only difference really is that we can't use Math.min.apply() on date strings. We can however use Array.prototype.reduce() in order to compare all the dates, it's just a bit more involved.

var dates = [
    "2022-09-15",
    "2022-10-10",
    "2022-12-01"
];

var inputDate = "2022-09-29";

var datesAfter = dates.filter(x => x > inputDate);
var closestDate = dates.reduce((a, b) => a > b ? a : b);
console.log(closestDate);
Jesse
  • 1,386
  • 3
  • 9
  • 23