【问题标题】:GMT time String to DateTime in dart [duplicate]GMT时间字符串到飞镖中的日期时间[重复]
【发布时间】:2021-05-26 03:52:06
【问题描述】:

我想在 dart 中将字符串转换为 DateTime。我试过了,

String timeVal = "Thu, 25 Mar 2021 19:29:28 GMT";
DateTime.parse(timeVal);

但遇到异常,

FormatException(FormatException:日期格式无效,2021 年 3 月 25 日星期四 格林威治标准时间 19:29:28)

如何在 dart 中将这样的字符串值转换为DateTime

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以使用intl 包:

import 'package:intl/intl.dart';

void main() {
  final string = 'Thu, 25 Mar 2021 19:29:28 GMT';
  final formatter = DateFormat('EEE, d MMM yyyy HH:mm:ss');
  final dateTime = formatter.parse(string);
  print(dateTime); // prints : 2021-03-25 19:29:28.000
}

有关详细信息,请参阅此 answerDartFormat 类文档。

请注意,示例中解析的DateTime 没有正确的时区(即dateTime.timeZoneName 将返回您的本地时区)。

【讨论】:

    猜你喜欢
    • 2019-11-11
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多