如何在 flutter 中将 RSS pubDate 字符串转换为 DateTime?

How to convert RSS pubDate string to DateTime in flutter?

我是 kei,这是我在 Whosebug 中的第一个问题。

我想将 RSS pubDate 字符串(像这样 → "Tue, 28 Jul 2020 22:00:00 +0900")转换为 DateTime 在 Flutter 中。

我在这个问题上花了 5 个小时...请帮助我。

在 Dart 中,您可以使用 parse 方法将其更改为 DateTime 数据。

var parsedDate = DateTime.parse('2020-07-28 22:00:00.000');

注意

parse方法有自己的输入规则。基于 the documentation :

The accepted inputs are currently:

  • A date: A signed four-to-six digit year, two digit month and two digit day, optionally separated by - characters. Examples:
    "19700101", "-0004-12-24", "81030-04-01".

  • An optional time part, separated from the date by either T or a space. The time part is a two digit hour, then optionally a two digit minutes value, then optionally a two digit seconds value, and then
    optionally a '.' or ',' followed by at least a one digit second
    fraction. The minutes and seconds may be separated from the previous
    parts by a ':'. Examples: "12", "12:30:24.124", "12:30:24,124",
    "123010.50".

  • An optional time-zone offset part, possibly separated from the previous by a space. The time zone is either 'z' or 'Z', or it is a
    signed two digit hour part and an optional two digit minute part. The sign must be either "+" or "-", and can not be omitted. The minutes
    may be separated from the hours by a ':'. Examples: "Z", "-10",
    "+01:30", "+1130".

因此您需要根据上述规则重新排列输入。

接受字符串的例子

2012-02-27 13:27:00

2012-02-27 13:27:00.123456789z

2012-02-27 13:27:00,123456789z

20120227 13:27:00

20120227T132700

20120227

+20120227

2012-02-27T14Z

2012-02-27T14+00:00

-123450101 00:00:00 Z: 年份-12345.

2002-02-27T14:00:00-0500:与2002-02-27T19:00:00Z

相同