Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Unhandled Exception: FormatException: Invalid date format [duplicate]

Tags:

flutter

dart

I am trying to format my date and time.

My code

    String dateStart = data[i]['notification_date'];
    DateTime input = DateTime.parse(dateStart);
    String datee = DateFormat('hh:mm a').format(input);

its showing error Unhandled Exception: FormatException: Invalid date format

right now its look like this 22-04-2021 05:57:58 PM

like image 831
rameez khan Avatar asked Nov 16 '25 07:11

rameez khan


1 Answers

You have an issue in following line:

DateTime input = DateTime.parse(dateStart);

Thing is that default parse method does not support '22-04-2021 05:57:58 PM' format, as it is not standard. You should specify its format to parse like this:

  String dateStart = '22-04-2021 05:57:58 PM';
  DateFormat inputFormat = DateFormat('dd-MM-yyyy hh:mm:ss a');
  DateTime input = inputFormat.parse(dateStart);
  String datee = DateFormat('hh:mm a').format(input);
like image 115
Alex Radzishevsky Avatar answered Nov 17 '25 20:11

Alex Radzishevsky



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!