mui/material-ui-pickers

maxDate for DatePicker doesn't work

AndrejGajdos opened this issue ยท 4 comments

maxDate props for DatePicker doesn't work.

Current Behavior ๐Ÿ˜ฏ

If I use maxDate={new Date()}, I am able to choose date after current date.

Steps to Reproduce ๐Ÿ•น

https://codesandbox.io/s/mui-date-picker-title-forked-48s8c?file=/src/demo.tsx

Your Environment ๐ŸŒŽ

Tech Version
@material-ui/core v4.11.3
@material-ui/pickers v4.0.0-alpha.12
React
Browser
TypeScript
etc.

@AndrejGajdos
I had the same issue.
You cannot use a function for the maxDate param.

if you pass a variable/constant it is working:

 const myMaxDate = new Date('2020-01-01');
 return (
         ...
        <DatePicker
          ...
          maxDate={myMaxDate}
        />
        ...
  );

@tobzilla90 it doesn't make any difference codesandbox

@AndrejGajdos
now you applied a string as param. But the input type is Date.

You can choose a Date after maxDate but then the validator will show that the selected date is not within the range (See that the label and the line gets displayed red). The Picker UI is only limited by the year for the DatePicker.

If you want to set the date directly here is how to do this:

       <DatePicker
          ...
          maxDate={new Date("2020-08-18")}
        />

The return of new Date() is Date type with the current date. There is no difference between my original codesandbox and maxDate={new Date("2020-08-18")}.

Check this codesandbox and console log.