[英]How to Disable today's date using date pickers - material ui
I'm working on a react project where I'm using material-ui-date-pickers and i want to disable today's date since the project is based on manufacturing products, expiration date of products and billing.我正在做一个反应项目,我正在使用material-ui-date-pickers并且我想禁用今天的日期,因为该项目基于制造产品、产品的到期日期和计费。 Since Manufacturing date and expiration date cannot be same,I want to disable today's date for expiration date.
由于制造日期和到期日期不能相同,我想禁用今天的到期日期。 I see props
minDate, maxDate
can be used, but is it possible to disable today's date using the same?我看到道具
minDate, maxDate
可以使用,但是否可以使用相同的禁用今天的日期? Below is my code.下面是我的代码。
<MuiPickersUtilsProvider utils={MomentUtils}>
<KeyboardDatePicker
autoOk={true}
variant='inline'
inputVariant='outlined'
format={dateFormat}
fullWidth
name='expirationDate'
value={expirationDate}
onChange={ (date) => handleDate(date)}
placeholder={dateFormat}
size='small'
disableFuture={true}
views={['date', 'month', 'year']}
/>
</MuiPickersUtilsProvider>
Yes, this be can done using JavaScript date method and using the props minDate, maxDate
.是的,这可以使用 JavaScript 日期方法和使用道具
minDate, maxDate
。
First declare the new date using首先使用声明新日期
const today = new Date();
and in maxDate
prop subtract today's date by 1 to get yesterday's date and give it as maxDate
.并在
maxDate
道具中将今天的日期减去 1 以获得昨天的日期并将其作为maxDate
。
maxDate={today.setDate(today.getDate() - 1)}
So your complete code will be所以你的完整代码将是
<MuiPickersUtilsProvider utils={MomentUtils}>
<KeyboardDatePicker
autoOk={true}
variant='inline'
inputVariant='outlined'
format={dateFormat}
fullWidth
name='expirationDate'
value={expirationDate}
onChange={ (date) => handleChangeDate(date)}
placeholder={dateFormat}
size='small'
disableFuture={true}
views={['date', 'month', 'year']}
maxDate={today.setDate(today.getDate() - 1)}
/>
</MuiPickersUtilsProvider>
Just add diablePast for the element KeyboardDatePicker只需为元素 KeyboardDatePicker 添加 diablePast
<MuiPickersUtilsProvider utils={MomentUtils}> <KeyboardDatePicker autoOk={true} diablePast variant='inline' inputVariant='outlined' format={dateFormat} fullWidth name='expirationDate' value={expirationDate} onChange={ (date) => handleDate(date)} placeholder={dateFormat} size='small' disableFuture={true} views={['date', 'month', 'year']} /> </MuiPickersUtilsProvider>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.