You need to enable JavaScript to run this app. 如何隐藏Material UI中datetime-local类型TextField的分钟选择项
优惠活动
大模型
产品
解决方案
定价
更多

如何隐藏Material UI中datetime-local类型TextField的分钟选择项

解决方案

要隐藏datetime-local类型输入框的分钟选择区域,可通过设置原生step属性实现,该属性单位为秒,设置为3600(即1小时)时,时间选择精度会限定到小时级别,自动隐藏分钟、秒选择模块。

在Material UI的TextField中需将step属性传递给内部原生input元素,修改后的代码如下:

<TextField
    label="From"
    type="datetime-local"
    InputLabelProps={{
        shrink: true,
    }}
    inputProps={{
        step: 3600
    }}
/>

补充说明

  • 该方案依赖浏览器对datetime-local的原生实现,Chrome、Edge、Safari 14.1及以上版本均支持该特性,低版本浏览器可能存在兼容差异
  • 如需更强的自定义能力和跨端一致性,推荐使用MUI官方的DateTimePicker组件(来自@mui/x-date-pickers扩展包),可通过views属性直接指定要展示的时间维度:
// 需先完成@mui/x-date-pickers的本地化配置
<DateTimePicker
  label="From"
  views={['year', 'month', 'day', 'hours']}
/>

内容的提问来源于stack exchange,提问作者Alexey Surovenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 09:54:04