0

I have a datepicker that needs to take the date "12/31/9999" even though maxDate is set to today.

enter image description here

I have it type in like this. However, when I run the comparison in the code, it returns false.

private CheckDate(form: FormGroup, group: any) {
     if (form.controls["date"].value == new Date("12/31/9999")) {
         //form.controls["date"].valid = true;
         (group.questions["date"] as DateTimePickerQuestion).maxDate = null;
     }
}

However, when I compare form.controls["date"].value to new Date("12/31/9999") it comes back as false and doesn't execute my code in the middle.

Why is that? Here is a copy/paste of the immediate window checking the values

new Date("12/31/9999")
Fri Dec 31 9999 00:00:00 GMT-0500 (Eastern Standard Time) {}
form.controls["date"].value
Fri Dec 31 9999 00:00:00 GMT-0500 (Eastern Standard Time) {}
form.controls["date"].value == new Date("12/31/9999")
false
Soulzityr
  • 406
  • 1
  • 8
  • 26
  • Yes it does thank you, but do I have to cast form.controls["date"].value to a DateTime in order to use getTime()? – Soulzityr Nov 03 '20 at 18:14
  • It appears as though `form.controls["date"].value` is returning a `Date` object already. You can verify by running `form.controls["date"].value instanceof Date` and seeing if it returns true. If so, just call `form.controls["date"].value.getTime()`. If not just call `new Date(form.controls["date"].value).getTime()`. – Heretic Monkey Nov 03 '20 at 18:17

1 Answers1

0

I agree with what Heretic Monkey advised. It seems that you are trying to compare two separate things. Try transforming both values to a string or to a date and go from there. What I normally do to satanize date formats before comparing them is using datePipe (because sometimes the formats are also different, as in date format - e.g:

01/10/2020 - First of October

01/10/2020 - 10th of January

So make sure you are comparing the same type of objects and the same type of date format! :)

Rodolfo Spier
  • 176
  • 10
  • 1
    They are both date objects. Heretic Monkey actually thought so too. I managed to make the comparison with the link he gave above once I verified both were Dates. Thank you for the response! – Soulzityr Nov 04 '20 at 17:31