Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter HTTP date strings with DateFormat

Tags:

flutter

intl: ^0.16.1

Can't seem to parse HTTP header date field with flutter DateFormat/HttpDate

here is what I tried:

          var date = HttpDate.parse("Thu, 11 Feb 2021 10:53:15 +0200");

return error:

Invalid HTTP date Thu, 11 Feb 2021 10:53:15 +0200

Note: the string was received by package:googleapis/gmail/v1.dart

      final zFormatDateEmailRFC2822 = DateFormat("EEE, d MMM yyyy HH:mm:ss Z");
      var date = zFormatDateEmailRFC2822.parse("Thu, 11 Feb 2021 10:53:15 +0200");

return error:

Trying to read EEE from Thu, 11 Feb 2021 10:53:15 +0200 at position 0

The following is an example from https://api.flutter.dev/flutter/intl/DateFormat-class.html

          final fmt = DateFormat('EEE, MMM d, ''yy'); 
          var date = fmt.parse("Wed, Jul 10, '96");

return error:

Trying to read EEE from Wed, Jul 10, '96 at position 0

      final fmt = DateFormat("d MMM yyyy");
      var date = fmt.parse("11 Feb 2021");

return error:

Trying to read MMM from 11 Feb 2021 at position 3

      final fmt = DateFormat("MMM");
      var date = fmt.parse("Feb");

return error:

Trying to read LLL from Feb at position 0

The only thing that works for me is:

          var dateStr = "Thu, 11 Feb 2021 10:53:15  +0200";
          RegExp regExp = RegExp(r"(.*, \d+ .* \d+ \d+:\d+:\d+)");
          if (regExp.hasMatch(dateStr)) {
            var groups = regExp.firstMatch(dateStr);
            var date = HttpDate.parse(groups[1] + " GMT");
            print("date $date >${HttpDate.format(date)}<");
          }
like image 906
Elia Weiss Avatar asked Aug 16 '26 10:08

Elia Weiss


1 Answers

Try it:

final fmt = DateFormat("d MMM yyyy", "en_US");
var date = fmt.parse("11 Feb 2021");
like image 70
Bobur Abduxalilov Avatar answered Aug 19 '26 06:08

Bobur Abduxalilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!