Material-UI:如何禁用日期时间本地文本字段中的先前日期

San*_*ana 4 javascript reactjs material-ui

我的目标是禁用过去的几天,并且不应该允许您选择,我已经尝试过但没有成功。

这是代码:

<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}
/>
Run Code Online (Sandbox Code Playgroud)

不知道我哪里错了。任何人都可以帮我解决这个问题吗?

Nea*_*arl 6

要禁用前一个日期,您可以使用min的属性input。以下是禁止在 之前的日期的方法2021-02-20T00:00

<TextField
  type="datetime-local"
  inputProps={{
    min: "2021-02-20T00:00"
  }}
/>
Run Code Online (Sandbox Code Playgroud)

要禁用以前的日期,您可以实例化一个新Date对象,该对象默认为当前日期并将其传递给min属性:

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

现场演示

编辑 67032222/无法禁用日期时间本地文本字段中的先前日期