formatTime method

String formatTime({
  1. String pattern = 'HH:mm:ss',
})

Formats the current date-time object into a time string based on a given pattern.

This method is a convenience wrapper around format(), allowing you to format only the time portion of a DateTime object.

Supported Time Placeholders:

  • HH → 24-hour format (e.g., 17)
  • H → 24-hour format without leading zero (e.g., 5)
  • hh → 12-hour format (e.g., 05)
  • h → 12-hour format without leading zero (e.g., 5)
  • mm → Two-digit minutes (e.g., 09)
  • m → Single-digit minutes (e.g., 9)
  • ss → Two-digit seconds (e.g., 07)
  • s → Single-digit seconds (e.g., 7)
  • SSS → Milliseconds (e.g., 007)
  • a → AM/PM (e.g., PM)

Example Usage:

final date = DateTime(2025, 2, 5, 17, 54, 30);
print(date.formatTime()); // Default: "17:54:30"
print(date.formatTime(pattern: 'hh:mm a')); // Output: "05:54 PM"

Implementation

String formatTime({String pattern = 'HH:mm:ss'}) {
  return format(pattern: pattern);
}