在 flutter 中格式化日期,以便月份用文字表示

Format date in flutter so that the month is in words

我有一个 DateTime 实例,我已将其格式化为字符串,但我希望月份用文字而不是数字书写,例如“2021 年 6 月 1 日”而不是“2021 年 6 月 1 日”

DateTime _date = DateTime.now();

print ("${_date.toLocal().day} ${_date.toLocal.month} ${_date.toLocal().year}";)

为此使用 intl 包。

print(DateFormat('d MMMM y').format(DateTime.now()));

我之前已经按照答案 进行了操作,并且有效。

TLDR;

  1. 从此处获取 intl 依赖项 pubdev-intl

  2. 通过将依赖项添加到 pubspec.yaml 来导入依赖项。 </p> <p>进口'package:intl/intl.dart';</p> <p>列出月份 = ['jan', 'feb', 'mar', 'apr', 'may','jun','jul','aug','sep','oct','nov','dec']; var now = new DateTime.now(); current_mon = now.month; 打印(月[current_mon-1]);</p> </li> </ol>