Home > Enterprise >  Flutter custom date format
Flutter custom date format

Time:10-27

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

CodePudding user response:

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

CodePudding user response:

Just checked in flutter docs. Your date format is not good. For converting string to date.

The following date format is required,

"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
" 20120227"
"2012-02-27T14Z"
"2012-02-27T14 00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

And you can convert that into the format like below

var date1 = DateFormat('dd-MM-yyyy hh:mm:ss').parse("27-10-2022 11:02:50");
var date2 = DateFormat('yyyy-MM-dd hh:mm:ss').format(date1);


print( DateFormat('EEEE, MMMM dd, yyyy').format(date2));

CodePudding user response:

please try this, hope you will get the idea,

 print(DateFormat('dd-MM-yyyy HH:mm:ss').parse('27-10-2022 11:02:50'));
  • Related