0

I am working on a date comparison for our application. The goal is take the Start Date the user inputs, and check it against the Operator/Region Effective Date which acts as the date a new list of product prices can be used. We want to see if there is an Effective Date 7 days in the future on the Start Date. If so, we want to display a message and let them know the prices are changing soon.

I figure the best thing to do is add 7 days to the Start Date and then see if its less then or equal to the Effective Date, if so, that would mean it is within the range. I just dont know how to add 7 days to the Start Date. Everything I have tried is giving me an "Invalid Date" back. Any ideas?

Code I've Tried So Far

    public checkStartDates = () => {
    const checkDates = this.state.checkDate;
    const region = this.state.well.region;

    const d1 = new Date(checkDates.startDate + 7);

    const d2 = new Date(region.effectiveDate);

    console.log(d1, d2);
    if (d1 > d2) {
        alert ("Error!");
    }
}

    public checkStartDates = () => {
    const checkDates = this.state.checkDate;
    const region = this.state.well.region;

    const d1 = new Date(checkDates.startDate + 7);
    d1.setDate(d1.getDate() + 7);

    const d2 = new Date(region.effectiveDate);

    console.log(d1, d2);
    if (d1 > d2) {
        alert ("Error!");
    }
}
MambaForever
  • 359
  • 3
  • 11
  • 1
    Does this answer your question? [How to add days to Date?](https://stackoverflow.com/questions/563406/how-to-add-days-to-date) `const d1 = new Date(checkDates.startDate); d1.setDate(d1.getDate() + 7);` – WOUNDEDStevenJones Sep 09 '22 at 15:57

1 Answers1

0

The reason it doesn't work is startDate probably is not a number. So you are trying to add a number on onto something else which really isn't one. You already had it with the d1.setDate(d1.getDate() + 7); line. You don't need the + 7 on the date init.

Try this:


    const d1 = new Date(checkDates.startDate)
    d1.setDate(d1.getDate() + 7);

    const d2 = new Date(region.effectiveDate);

    console.log(d1, d2);
    if (d1 > d2) {
        alert ("Error!");
    }
adsy
  • 8,531
  • 2
  • 20
  • 31