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
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With