【问题标题】:Flutter - 打印日期的日期格式,如 MMMdy
【发布时间】:2022-01-23 13:02:05
【问题描述】:

我需要显示 2021 年 12 月 22 日这样的日期。我怎样才能做到这一点? DateFormat.yMMMd().format(date) 这给出了 2021 年 12 月 22 日这样的日期。

【问题讨论】:

标签: android ios flutter dart


【解决方案1】:

是的,首先导入package:intl/intl.dart 然后:

 DateTime now = DateTime.now();
 String formatted = DateFormat('dd MMM yyyy').format(now);
 print(formatted); // prints 22 Dec 2021

【讨论】:

    【解决方案2】:

    您需要为此使用intl 包。 日期格式使用骨架工作。在这种情况下,我们需要的是 dd、MM 和 yyyy 骨架。

      final DateFormat formatter = DateFormat('dd MMM yyyy');
      print(formatter.format(date));
    

    假设您出于任何原因想要 22/Dec/2021,您只需将 dd/MMM/yyyy 复制到 DateFormat 即可,日期将根据需要返回。您可以获得所有骨骼的列表here。您在此处使用的yMMMd 函数在底层使用了骨架。这些只是比较常见的一些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2014-09-29
      • 2010-09-23
      • 2013-11-10
      相关资源
      最近更新 更多