Home > Blockchain >  How To Convert "EEE, MMM d, yyyy hh:mm aaa" DateTime Format to "yyyy-MM-dd HH:mm:ss&q
How To Convert "EEE, MMM d, yyyy hh:mm aaa" DateTime Format to "yyyy-MM-dd HH:mm:ss&q

Time:10-01

I am working in a Flutter Project using Dart where I have a String DateTime as Fri, Sep 30, 2022 01:10 PM that I want to convert in DateTime object with 2022-09-30 13:10:00 format.

For this, I tried the below snippet...

String inputDateTime = "Fri, Sep 30, 2022 01:10 PM";
DateFormat formatter = DateFormat('YYYY-MM-DD hh:mm:ss');
String formatted_inputDateTime = formatter.format(inputDateTime);
DateTime inputDateTime_DT = DateTime.parse(formatted_inputDateTime);

But getting error and now getting my required DateTime object.

How to fix this up to get required format of DateTime Object?

CodePudding user response:

By using intl package, try this:

DateTime tempDate = new DateFormat("EEE, MMM d, yyyy hh:mm aaa")
        .parse('Fri, Sep 30, 2022 01:10 PM');
String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(tempDate);
print('formattedDate = $formattedDate'); //2022-09-30 13:10:00
  • Related