日期控件有些时候,选择过去时间是没有意义的,比如送货时间【明日以后可以选择】
1.安装需要的组件:
import { MuiPickersUtilsProvider } from '@material-ui/pickers';
import { DatePicker } from '@material-ui/pickers';
import DateFnsUtils from '@date-io/date-fns';
import znLocale from 'date-fns/locale/zh-CN'
import moment from "moment";
2.组件
<MuiPickersUtilsProvider utils={DateFnsUtils} locale={znLocale}>
<DatePicker
className={classes.datePicker}
disablePast
disableToolbar
variant="inline"
inputVariant="standard"
format="yyyy/MM/dd"
id="hopedate"
minDate={DateFunPick}
label=""
value={selectedDate}
defaultValue={DateFun()}
onChange={setSelectedData}
/>
</MuiPickersUtilsProvider>
3.设定可选的最小日期
let today = new Date();
let formatY = "";
formatY += today.getFullYear();
formatY += today.getMonth() + 1 < 10 ? "0" + (today.getMonth() + 1) : today.getMonth() + 1;
formatY += today.getDate() + 1 < 10 ? "0" + today.getDate() + 1 : today.getDate() + 1;
const DateFunPick = moment(formatY).utcOffset(8).format('YYYY-MM-DD');
const [selectedDate, setSelectedData] = React.useState(moment(formatY).utcOffset(8).format('YYYY-MM-DD'));
4.效果如下