Flutter parse GMT

Viewed 771

Hello,I have a question! I want to parse Time, GMT to Milliseconds。 for example:Thu, 10 Oct 2019 02:05:09 GMT. I try DateTime.parse("Thu, 10 Oct 2019 02:05:09 GMT") i get this error

Exception caught by widgets library The following FormatException was thrown building GroupPage(dirty, state: _GroupPage#cb39c): invalid date format Thu, 10 oct 2019 02:05:09 GMT

3 Answers

You can use the HttpDate class to parse this.

Example:

DateTime parsedDate = HttpDate.parse("Thu, 10 Oct 2019 02:05:09 GMT");

DateTime.parse can parse only ISO 8601 dates, not an arbitrary ones. From docs:

The function parses a subset of ISO 8601 which includes the subset accepted by RFC 3339.

Examples of accepted strings:

"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456z"
"2012-02-27 13:27:00,123456z"
"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"

DateTime.parse method is not an option for your date string, unless you get it in ISO 8601 format.

Related