Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with DateFormat in dart: "Trying to read -YYYY from 17-04-2020 at position 10"

Tags:

flutter

dart

How to convert a string like '17-04-2020' which is in dd-MM-yyyy format to DateTime object in dart/flutter?

formatter = new DateFormat('dd-MM-YYYY');
formatter.parse(strDate);

I tried the above code and getting an error:

Trying to read -YYYY from 17-04-2020 at position 10
like image 300
Aman Kumar Avatar asked Apr 17 '20 12:04

Aman Kumar


People also ask

How do you format a date in flutter?

To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.


1 Answers

You need lower-case Y's on your date pattern, use this:

"dd-MM-yyyy"

You can check the docs here for available date patterns.

In case you only need two digits of the year, you can also use dd-MM-yy.

like image 165
George Avatar answered Oct 21 '22 02:10

George