【问题标题】:Weird part of DateTime.parse(data['datetime']) in DartDart 中 DateTime.parse(data['datetime']) 的奇怪部分
【发布时间】:2020-03-20 06:44:17
【问题描述】:

问题描述

我的数据库中有 020-03-20T14:16:27.189282+08:00 返回,我正在尝试使用 DateTime.parse() 将其转换为 2:16pm。但是,每次我转换时间,我都会得到6:16am

问题

请问我该如何解决这个问题?

我的代码

dateTime = `020-03-20T14:16:27.189282+08:00`
time = DateFormat.jm().format(DateTime.parse(dateTime));
print(time);

【问题讨论】:

  • 你得到6:16am,因为你使用的值包含一个UTC偏移量+08:00,顺便说一句。
  • 啊,我明白了。另一个词,对我来说是下午 2.16 点。我需要摆脱 +08:00 的偏移量。注意。

标签: flutter dart


【解决方案1】:

Dai,感谢您的小提示。

这个问题的解决方案是从字符串中删除偏移量。

为什么

结果始终采用当地时间或 UTC。 If a time zone offset other than UTC is specified, the time is converted to the equivalent UTC time.

在这里阅读https://api.dart.dev/stable/2.7.1/dart-core/DateTime/parse.html

已回答

String dateTime = `020-03-20T14:16:27.189282+08:00`;
int indexOfPlusMinusSymbol = dateTime .indexOf('+') >= 0
          ?dateTime .lastIndexOf('+')
          : dateTime .lastIndexOf('-');
String time = DateFormat.jm().format(DateTime.parse(dateTime..substring(0, indexOfPlusMinusSymbol)));
print(time);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2012-05-25
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多