4

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

rameez khan
  • 73
  • 1
  • 23
  • 68

1 Answers1

11

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);
Alex Radzishevsky
  • 3,416
  • 2
  • 14
  • 25