如何在 material-ui <textfield type="datetime-local" /> 中设置未来时间

how to set future time in material-ui <textfield type="datetime-local" />

我想将默认时间设置为比当前时间晚 4 小时,因此如果现在是 2021 年 10 月 31 日 3:00 上午,则默认时间应显示 2021 年 10 月 31 日 4:00 上午 对于像 2021 年 10 月 31 日 10:00 pm 这样的时间,它应该显示 2021 年 11 月 1 日 2:00 am

我尝试将当前时间加到 4,但它打破了夜间时间。


const currentDate = new Date();

  const dateTime = `${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}T${
    currentDate.getHours() + 3
  }:${currentDate.getMinutes()}`;


<TextField
 id="datetime-local"
 type="datetime-local"
 defaultValue={`${dateTime}`}
 InputLabelProps={{
     shrink: true,
     }}
 InputProps={{ inputProps: { min: `${dateTime}` } }}
 onChange={handleChange}
 />
 </div>

您可以使用 Date 对象的 setHoursgetHours 方法。

const currentDate = new Date();
const fourHoursLater = new Date(
  currentDate.setHours(currentDate.getHours() + 4)
);

codesandbox