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

Flutter/Dart:使用intl包的DateFormat如何去除日期前导零?

解决intl包DateFormat日期无前导零的问题

核心方案:使用d格式符+指定正确Locale

你遇到的问题本质是Locale设置导致的格式差异,d格式符本身就是用来输出无前导零的单数字日期(双数字自动保留两位),正确写法如下:

_standardDateFormat = DateFormat('MMM d, yyyy');

如果仍然出现前导零,说明当前默认Locale的日期规则强制补零,此时显式指定符合需求的Locale即可(比如英文环境的en_US):

_standardDateFormat = DateFormat('MMM d, yyyy', 'en_US');

备选手动处理方案

如果Locale设置无法解决,也可以手动拼接日期各部分,直接有效:

DateTime targetDate = DateTime(2023, 2, 1);
String formattedDate = '${DateFormat('MMM').format(targetDate)} ${targetDate.day}, ${targetDate.year}';
// 输出:Feb 1, 2023

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:45:45