7

I'm enable to do use DateFormat in flutter when am using the locale as 'ar' which is RTL locale .. here is my code :

String finalDatetime = '$dateString $timeString';
DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime);
String formattedDate = Intl.getCurrentLocale() == 'en' ? DateFormat('dd-MM-yyyy – hh:mm').format(tempDate)
:DateFormat.yMMMd('ar').format(tempDate);

I'm getting the error in this line :

DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime);

The error

FormatException: Trying to read yyyy from 2020-08-07 15:00 at position 0

NOTES

  • It works perfectly when the locale is set to 'en'
  • The date string is being fetched from a database which is always sent as 2020-08-07 15:00
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
  • 2
    You probably should [file an issue](https://github.com/dart-lang/intl/issues). As a workaround, since the fetched string is always in a fixed format, you could use [`Intl.withLocale`](https://pub.dev/documentation/intl/latest/intl/Intl/withLocale.html) to force parsing with the `'en'` locale (or [manually parse it with a regular expression](https://stackoverflow.com/a/61394854/)). – jamesdlin Aug 22 '20 at 19:46
  • The only one working solution for me: https://stackoverflow.com/questions/66570895/flutter-locale-date-string-to-datetime/71599205#71599205 – IvanPavliuk Mar 24 '22 at 12:10

1 Answers1

6

as Jamesdlin mentioned on his comment, using Intl.withLocale fixed the problem

DateTime tempDate =Intl.withLocale('en', () => new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime));
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116