0

I'm kei, this is my first question in stackoverflow.

I want to convert RSS pubDate string(like this → "Tue, 28 Jul 2020 22:00:00 +0900") to DateTime in Flutter.

I spent 5 hours on this problem...please help me.

1 Answers1

0

In Dart, you can change it to the DateTime data using the parse method.

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

NOTE

The parse method has its own input rules. Based on 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".

So you need to rearrange the input based on the rules above.

Here's the examples of accepted String

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: in the year -12345.

2002-02-27T14:00:00-0500: Same as 2002-02-27T19:00:00Z

hisam
  • 1,566
  • 1
  • 7
  • 21
  • Thank you for the detailed reply! Hmm.. so I must make a new input string from pubDate to fit in accepted String. Is there any way to make new input? – kei yonekura Jul 28 '20 at 23:27
  • Yes. And because I am really interested in this question, I've made a code to rearrange your string from `Tue, 28 Jul 2020 22:00:00 +0900` to `2020-07-28 22:00:00` and `parsed` it. You can find it here: https://gist.github.com/hisamafahri/332d6b8d650baf372b7179c0051febfe I assume my code will work fine because RSS PubDate has the rules on writing their DateTime syntax. https://www.w3.org/Protocols/rfc822/#z28 – hisam Jul 29 '20 at 00:00
  • I have to give you a very big thank you..it becomes a big help for me. – kei yonekura Jul 29 '20 at 11:06
  • It's my pleasure. – hisam Jul 29 '20 at 12:03