I have this schema in Yup that puts a minimum date constraint on the date field:
Yup.date()
.required(strings.RequiredField(strings.Invoice_Label_DueDate))
.min(new Date(), "Date cannot be in the past")
But if I select the current date, it still computes it to be in the past. Note that this doesn't happen when I do max(new Date(), ...)
. In this case, it includes all past dates upto and including the current date.
Edit: Here's an example of the problem https://codesandbox.io/s/jznz5vl7lw
You can also use:
const today = new Date();
today.setHours(0, 0, 0, 0)
then
.min(today, 'Date cannot be in the past')
If you want to set the minimum to be yesterday's date, you can declare a variable and set it to yesterday's date, for example (there are many other ways too):
const yesterday = new Date(Date.now() -86400000);
then:
.min(yesterday, "Date cannot be in the past")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With