formatTimeOfDay static method

String formatTimeOfDay(
  1. TimeOfDay? timeOfDay, {
  2. bool formatTo12Hr = true,
})

Implementation

static String formatTimeOfDay(TimeOfDay? timeOfDay, {bool formatTo12Hr = true}) {
  if (timeOfDay == null) return '';
  String addLeadingZeroIfNeeded(int value) {
    if (value < 10) {
      return '0$value';
    }
    return value.toString();
  }

  int hr = formatTo12Hr ? timeOfDay.hour % 12 : timeOfDay.hour;

  final String hourLabel = addLeadingZeroIfNeeded(hr);
  final String minuteLabel = addLeadingZeroIfNeeded(timeOfDay.minute);

  String suffix = formatTo12Hr
      ? (timeOfDay.hour > 11 ? ' PM' : ' AM')
      : '';

  return '$hourLabel:$minuteLabel$suffix';
}