Encountering an issue with comparing the date of birth object and today's date object using Moment.js. Even if the entered date is smaller than today's date, it still throws an error.
Below is the HTML code:
<div class="form-group datepicker">
<label for="dob">Date of Birth*</label>
<div class="row input-group">
<input
ngbDatepicker
#d="ngbDatepicker"
#dobF="ngModel"
class="form-control input-underline input-lg"
id="dob"
[(ngModel)]="dateOfBirth"
placeholder="yyyy-mm-dd"
name="dp"
[ngClass]="{
invalid:
(dobF.value === null || isString(dobF.value) || (dateOfBirth.year > dobYear || dateOfBirth.month > dobMonth || dateOfBirth.day > dobDay) ) && dobF.touched
}"
required
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="d.toggle()"
type="button"
></button>
</div>
</div>
<div
*ngIf="
(dobF.value === null || isString(dobF.value) || dateOfBirth.year > dobYear || dateOfBirth.month > dobMonth || dateOfBirth.day > dobDay ) && dobF.touched
"
class="error"
>
Please enter a valid date of birth.
</div>
</div>
And here is the TypeScript file where I defined my date of birth:
public dateOfBirth: { year: number; month: number; day: number };
public currentDate = moment().format("YYYY-MM-DD");
public dobYear: any;
public dobMonth: any;
public dobDay: any;
let obj = this.currentDate.split("-");
let obj2 = obj.map(Number);
this.dobYear = obj2[0];
this.dobMonth = obj2[1];
this.dobDay = obj2[2];
The error occurs due to today's month being 02, causing issues when entering a date like 2012-09-09 because 02<09. Any suggestions on how to resolve this issue would be greatly appreciated. Thank you!