formatDateTime method
Formats a DateTime object into a string.
showDate - Whether to include the date part.
showTime - Whether to include the time part.
showSeconds - Whether to include seconds in the time part.
use24HourFormat - Whether to use 24-hour format for time.
Implementation
String formatDateTime(DateTime dateTime,
{bool showDate = true,
bool showTime = true,
bool showSeconds = false,
bool use24HourFormat = true}) {
String result = '';
if (showDate) {
result += '${getMonth(dateTime.month)} ${dateTime.day}, ${dateTime.year}';
}
if (showTime) {
if (use24HourFormat) {
if (result.isNotEmpty) {
result += ' ';
}
result += '${dateTime.hour}:${dateTime.minute}';
if (showSeconds) {
result += ':${dateTime.second}';
}
} else {
if (result.isNotEmpty) {
result += ' ';
}
int hour = dateTime.hour;
if (hour > 12) {
hour -= 12;
result += '$hour:${dateTime.minute} $timePM';
} else {
result += '$hour:${dateTime.minute} $timeAM';
}
}
}
return result;
}