You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Flutter中的showDateTimePicker()切换为12小时制格式?

把showDateTimePicker切换为12小时制的方法

嘿,我能帮你搞定这个12小时制的设置!你用的应该是flutter_date_picker库吧?要让日期时间选择器以12小时制显示,只需要给showDateTimePicker添加两个关键配置项就可以了:

  • 配置dateFormat参数:使用包含hh(12小时制小时)和a(AM/PM标识)的格式字符串,比如"yyyy-MM-dd hh:mm a"
  • 保持locale: LocaleType.en,因为AM/PM的显示依赖英文区域设置

修改后的完整代码如下:

TextButton( 
  onPressed: () { 
    FocusScope.of(context).unfocus(); 
    DatePicker.showDateTimePicker( 
      context, 
      showTitleActions: true, 
      onConfirm: widget.onChangeDateSched, 
      locale: LocaleType.en,
      // 核心配置:设置12小时制的显示格式
      dateFormat: "yyyy-MM-dd hh:mm a",
      // 可选:进一步控制时间选择器的24小时制开关
      timePickerTheme: const TimePickerTheme(
        is24HourMode: false,
      ),
    ); 
  },
)

这样设置之后,选择器就会以12小时制展示时间,并且确认后返回的时间字符串也会带有AM/PM标记。如果你需要自定义日期部分的格式,只调整dateFormat里的日期部分就好,时间部分保留hh:mm a即可。

内容的提问来源于stack exchange,提问作者Adrian Alejo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 13:07:31