formatDateFromDateTime method

String formatDateFromDateTime({
  1. bool hasHours = false,
})

Formats the DateTime as dd/MM/yyyy or dd/MM/yyyy HH:mm depending on hasHours.

Example:

DateTime(2023, 5, 10).formatDateFromDateTime(); 
// "10/05/2023"

DateTime(2023, 5, 10, 14, 30).formatDateFromDateTime(hasHours: true); 
// "10/05/2023 14:30"

Implementation

String formatDateFromDateTime({bool hasHours = false}) {
  final day = this.day.toString().padLeft(2, '0');
  final month = this.month.toString().padLeft(2, '0');
  final year = this.year.toString();

  if (!hasHours) {
    return '$day/$month/$year';
  } else {
    final hour = this.hour.toString().padLeft(2, '0');
    final minute = this.minute.toString().padLeft(2, '0');
    return '$day/$month/$year $hour:$minute';
  }
}