【问题标题】:In Flutter i got a DateTime [yyyy-MM-dd 00:00:00.000] how can i transfer that to be [yyyy-MM-dd] only?在 Flutter 中,我得到了一个 DateTime [yyyy-MM-dd 00:00:00.000] 我如何才能将其仅转换为 [yyyy-MM-dd]?
【发布时间】:2020-07-08 11:08:35
【问题描述】:

这是我已经将其格式化为 [yyyy-MM-dd] 但它还包含 00:00:00 的代码:


    child: Text(
         _selectedDate == null
     ? 'No Date Choosen!'
     : '${DateFormat('yyyy-MM-dd').format(_selectedDate)}',
     style: TextStyle(
     color: Colors.white, fontSize: 16),),

【问题讨论】:

标签: datetime flutter dart tostring formatted


【解决方案1】:

您需要创建一个新的 DateFormat 对象。

final df = new DateFormat('yyyy-MM-dd');
Text(_selectedDate == null ? 'No Date Choosen!' : '${df.format(_selectedDate)}');

【讨论】:

    【解决方案2】:

    以下是如何格式化日期的示例:

      final DateTime now = DateTime.now();
      final DateFormat formatter = DateFormat('yyyy-MM-dd');
      final String formatted = formatter.format(now);
      print(formatted);
    

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2016-12-25
      • 2020-11-11
      • 2016-06-21
      相关资源
      最近更新 更多