convertMinutesToPattern property

String get convertMinutesToPattern

Converts an integer value representing minutes to a formatted string pattern.

The this parameter is the total number of minutes to be converted.

Returns a string representation of the time in the format "Xh Ym", "Ym", or "Xh".

Example:

int minutes = 160;
String time = minutes.convertMinutesToPattern;
print(time); // Output: "2h 40m"

int minutesOnly = 45;
String timeOnlyMinutes = minutesOnly.convertMinutesToPattern;
print(timeOnlyMinutes); // Output: "45m"

int hoursOnly = 120;
String timeOnlyHours = hoursOnly.convertMinutesToPattern;
print(timeOnlyHours); // Output: "2h"

Implementation

String get convertMinutesToPattern {
  int hours = this ~/ 60; // Get the whole number of hours
  int minutes = this % 60; // Get the remaining minutes

  String? fmtTime;

  if (hours > 0 && minutes > 0) {
    fmtTime = '${hours} Hours ${minutes} Minutes';
  } else if (hours == 0 && minutes > 0) {
    fmtTime = '${minutes} Minutes';
  } else if (hours > 0 && minutes == 0) {
    fmtTime = '${hours} Hours';
  }

  return fmtTime ?? '';
}