Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yup/Formik min date does not include current date

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

like image 341
blankface Avatar asked Feb 28 '19 02:02

blankface


2 Answers

You can also use:

const today = new Date();
today.setHours(0, 0, 0, 0)

then

.min(today, 'Date cannot be in the past')
like image 106
John Schlachtenhaufen Avatar answered Oct 30 '22 03:10

John Schlachtenhaufen


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")
like image 29
YoniWitz Avatar answered Oct 30 '22 03:10

YoniWitz