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

Flutter中如何使用intl将DateTime转换为本地时间?

用Flutter intl包显示24小时制本地时间

你当前代码里的DateFormat('hh:mm')用的是12小时制的小时格式符hh,要显示24小时制时间,只需要把hh替换成HH即可——intl包中HH代表24小时制的小时(范围00-23),而hh是12小时制(范围01-12)。

修改后的代码如下:

Text(
   DateFormat('HH:mm').format(trackballDetails.point!.x),
   style: TextStyle(
      color: Colors.white,
      fontSize: 10.sp,
      fontFamily: "Mont"
   ),
)

另外,DateFormat默认会使用设备的本地时区来格式化时间,所以不需要额外配置就能显示本地时间的24小时制格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 22:00:54