,reactjs,datetime,datepicker,material-ui"/>

首页 > 解决方案 > 如何在material-ui中设置未来时间

问题描述

我想将默认时间设置为比当前时间晚 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>

标签: reactjsdatetimedatepickermaterial-ui

解决方案


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

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

密码箱


推荐阅读