format method

String format(
  1. String formatStr
)

Formats the duration with a custom format string.

Supported tokens:

  • Y or YY - Years
  • M or MM - Months
  • D or DD - Days
  • H or HH - Hours
  • m or mm - Minutes
  • s or ss - Seconds

Implementation

String format(String formatStr) {
  var result = formatStr;

  // Calculate total values (kept for potential future use)
  // final totalDays = this.days + (weeks * 7) + (months * 30) + (years * 365);
  // final totalHours = hours + (totalDays * 24);
  // final totalMinutes = minutes + (totalHours * 60);
  // final totalSeconds = seconds + (totalMinutes * 60);

  // Replace tokens
  result = result.replaceAll('YY', years.toString().padLeft(2, '0'));
  result = result.replaceAll('Y', years.toString());

  result = result.replaceAll('MM', months.toString().padLeft(2, '0'));
  result = result.replaceAll('M', months.toString());

  result = result.replaceAll('DD', days.toString().padLeft(2, '0'));
  result = result.replaceAll('D', days.toString());

  result = result.replaceAll('HH', hours.toString().padLeft(2, '0'));
  result = result.replaceAll('H', hours.toString());

  result = result.replaceAll('mm', minutes.toString().padLeft(2, '0'));
  result = result.replaceAll('m', minutes.toString());

  result = result.replaceAll('ss', seconds.toString().padLeft(2, '0'));
  result = result.replaceAll('s', seconds.toString());

  return result;
}