getDateTimeStr function
- String dateTimeStr, {
- DateFormatType formatType = DateFormatType.defaultFormat,
Get the given date and time in a specific format.
The default for the optional argument is the most readily human readable,
suggested as 4 Jul 2025 8:45 AM
. The leading zero on the day and hour are
dropped, as well as seconds. (20250714 gjw)
Implementation
String getDateTimeStr(
String dateTimeStr, {
DateFormatType formatType = DateFormatType.defaultFormat,
}) {
String pattern;
switch (formatType) {
case DateFormatType.longDate:
pattern = 'd MMM yyyy';
break;
case DateFormatType.longDateTime:
pattern = 'dd/MM/yyyy hh:mm:ss a';
break;
case DateFormatType.defaultFormat:
// default:
pattern = 'd MMM yyyy h:mm a';
}
final dateFormat = DateFormat(pattern);
return dateFormat.format(DateTime.parse(dateTimeStr));
}