flutterdartdatetimeflutter-animationflutter-datetime-picker

Flutter custom date format


I am facing issue while formatting the date to custom format. I need to convert date yyyy-MM-dd HH:mm:ss ===> EEEE, MMM dd, yyyy

For example I am getting date from server 27-10-2022 11:02:50, and I need to convert it to Thursday, October 27, 2022


Solution

  • Getting Date format is "dd-MM-yyyy HH:mm:ss" and the desire format will be "EEEE, MMMM dd, yyyy"

      final data = "27-10-2022 11:02:50";
      final format = DateFormat("dd-MM-yyyy HH:mm:ss");
      final DateTime result = format.parse(data);
      print(result); //2022-10-27 11:02:50.000
    
      final newFormatter = DateFormat("EEEE, MMMM dd, yyyy");
      final newFormatString = newFormatter.format(result);
      print(newFormatString); // Thursday, October 27, 2022
    

    I am using intl package