【问题标题】: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);