Age 18+ Validation with Ionic & Angular issue

So, I managed to get it to work but the year 2004 returns under 18. Let's say I set the year to 01/13/2004 it returns the age as 18 but also returns they're under 18. This also goes with any day within the month of January that meets the requirements for being 18. Here's the function for checking age & date. My best guess at this point is possibly timezone issue?

GetAge(date: string): number {
    let today = new Date();
    let birthDate = new Date(date);
    let age = today.getFullYear() - birthDate.getFullYear();
    let month = today.getMonth() - birthDate.getMonth();
    
    if (month < 0 || (month === 0 && today.getDate() < birthDate.getDate())) {
      age--;
    }
    console.log('Age: ' + age + '\nBirthday: ' + birthDate);
    return age;
  }

  AgeCheck(controlName: string): ValidatorFn {
    return (controls: AbstractControl) => {
      const control = controls.get(controlName);
      
      if (control?.errors && !control.errors['under18']) {
        return null;
      } 
  
      if (this.GetAge(control?.value) <= 18) {
        return { under18: true }
      } else {
        return null;   
      }
    }
  }

I'm using this as the date selection input

<ion-input type="date" formControlName="dateofbirth"></ion-input>

And here's an example of the FormBuilder

this.credentials = this.fb.group({
      dateofbirth: ['', [Validators.required]],
    }, {validator: [this.AgeCheck('dateofbirth')]});

Screenshot of console out put Console Out put


The BEST way is to use Moment.js like:

npm install moment --save  

and

import * as moment from 'moment';

...

age18Check(birthday: Date) {
  return moment(birthday).add(18, 'years') <= moment();
}

In this way you can also check the hour if you insert it in the birthday(Date). But if you have problems with hours you can also this:

age18Check(birthday: Date) {
  return
    moment(birthday).add(18, 'years').format('DD/MM/YYYY') <= 
    moment().format('DD/MM/YYYY');
}

I hope you was looking for this!