formatDateRange function

String formatDateRange(
  1. {required DateTime startDateTime,
  2. required DateTime endDateTime}
)

Implementation

String formatDateRange(
    {required DateTime startDateTime, required DateTime endDateTime}) {
  if (startDateTime.month == endDateTime.month) {
    // Format the dates with day number only
    final startDay = DateFormat('d').format(startDateTime);
    final endDay = DateFormat('d').format(endDateTime);
    return "${DateFormat('MMMM').format(startDateTime)} $startDay-$endDay";
  } else {
    // Format dates with full month names if months differ
    final startDay = DateFormat('d').format(startDateTime);
    final endDay = DateFormat('d').format(endDateTime);
    return "${DateFormat('MMMM').format(startDateTime)} $startDay - ${DateFormat('MMMM').format(endDateTime)} $endDay";
  }
}