0

Having two dates, firstDate and secondDate, I want to check if a new one is situated between these two but I keep getting errors.

My approach:

export class AppComponent {
  first = "20171212";
  second = "20181212";

  firstDate = new Date(this.first);
  secondDate = new Date(this.second);

  check = "20180101";
  checkDate = new Date(this.check);

  if( this.checkDate > this.firstDate && this.today < this.secondDate) {
      return true;

  }
}

The error message says that it cannot be used the dot notation in this.checkDate and the others.

Any ideas why is this wrong?

Leo Messi
  • 5,157
  • 14
  • 63
  • 125
  • Re `new Date(this.first)`, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 27 '19 at 20:32

1 Answers1

0

Any kind of logic or function needs to be added within a class method. Add your logic to a class method and call that method instead. For example:

export class AppComponent {
    first = "20171212";
    second = "20181212";

    firstDate = new Date(this.first);
    secondDate = new Date(this.second);
    check = "20180101";

    dateChecker() {
        checkDate = new Date(this.check);

        if( this.checkDate > this.firstDate && this.checkDate < this.secondDate) {
            return true;
        }
    }
}

You would then call the dateChecker() method instead.

joshua miller
  • 1,686
  • 1
  • 13
  • 22
  • Your question does not state how you are trying to call this logic. Include that and I will change my answer accordingly. – joshua miller Oct 27 '19 at 11:50
  • `new Date(this.first)` is strongly discouraged. `new Date("20170112")` produces an invalid date in Safari, Firefox and Chrome (at least). – RobG Oct 27 '19 at 20:36