Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI: How to disable previous dates in the datetime-local TextField

My objective is to disable the past days and should not allow you to select, I've tried but didn't worked.

Here is the code:

<TextField
  variant="outlined"
  id="datetime-local"
  label="Select Date and Time"
  placeholder="Select Date and Time"
  type="datetime-local"
  value={this.state.DateTime}
  InputLabelProps={{
    shrink: true,
  }}
  onChange={this.HandleChange}
/>

Don't know where I'm going wrong. Can anyone help me in this query?

like image 945
Sanjana Avatar asked Oct 30 '25 05:10

Sanjana


1 Answers

To disable the previous date, you can use the min attribute of input. Here is how you disallow dates before 2021-02-20T00:00:

<TextField
  type="datetime-local"
  inputProps={{
    min: "2021-02-20T00:00"
  }}
/>

To disable previous dates, you can instantiate a new Date object, which defaults to current date and pass it to the min attribute:

<TextField
  type="datetime-local"
  inputProps={{
    // only needs the first 16 characters in the date string
    min: new Date().toISOString().slice(0, 16),
  }}
/>

Live Demo

Edit 67032222/unable-to-disable-previous-dates-in-the-datetime-local-textfield

like image 100
NearHuscarl Avatar answered Nov 01 '25 18:11

NearHuscarl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!