【问题标题】:Flutter Unhandled Exception: FormatException: Invalid date format [duplicate]颤振未处理的异常:FormatException:无效的日期格式[重复]
【发布时间】:2021-07-16 21:00:26
【问题描述】:

我正在尝试格式化我的日期和时间。

我的代码

    String dateStart = data[i]['notification_date'];
    DateTime input = DateTime.parse(dateStart);
    String datee = DateFormat('hh:mm a').format(input);

它显示错误 Unhandled Exception: FormatException: Invalid date format

现在看起来像这样22-04-2021 05:57:58 PM

【问题讨论】:

  • 看起来不正确的格式中的“a”是什么

标签: flutter dart


【解决方案1】:

您在以下行中遇到问题:

DateTime input = DateTime.parse(dateStart);

问题是默认解析方法不支持“22-04-2021 05:57:58 PM”格式,因为它不是标准的。您应该像这样指定它的解析格式:

  String dateStart = '22-04-2021 05:57:58 PM';
  DateFormat inputFormat = DateFormat('dd-MM-yyyy hh:mm:ss a');
  DateTime input = inputFormat.parse(dateStart);
  String datee = DateFormat('hh:mm a').format(input);

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2021-08-07
    • 1970-01-01
    • 2019-11-27
    • 2019-10-05
    • 2021-07-31
    • 2022-11-14
    • 2021-07-16
    • 2020-12-05
    相关资源
    最近更新 更多