reactjs - 如何在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>
解决方案
您可以使用Date 对象中的setHours
和getHours
方法。
const currentDate = new Date();
const fourHoursLater = new Date(
currentDate.setHours(currentDate.getHours() + 4)
);
推荐阅读
- java - 如何覆盖另一个应用程序的默认方向?
- ssh - 在启动 VNC 会话时询问我第二个“密码”时,MobaXterm 是什么意思?
- php - 如何在 Laravel 8 中的控制器和刀片模板中创建所需的下拉选择字段
- react-native - React native - 如何在所有子视图完成渲染后知道视图的高度?
- java - 来自 Java 的 Runtime.getRuntime.exec 139 (SIGSEGV) 用于 C 程序
- laravel - DateTime::__construct():无法在位置 0 (2) 解析时间字符串 (22/03/1983):意外字符
- javascript - 如何在 useEffect() React Hook 中使用 await 关键字
- apache-nifi - 如何从 nifi 中的 csv 文件中删除重复的列
- linux - 读取输入而不指定输入长度
- c# - 使自托管 WCF 数据服务主机名感知