format method

String format(
  1. String format, {
  2. String? locale,
})

Formats this DateTime using the specified format string

format The format string (uses DateFormat patterns) locale Optional locale for formatting

Returns the formatted string

Example:

final date = DateTime.now();
print(date.format('yyyy-MM-dd')); // "2023-12-25"
print(date.format('MMM dd, yyyy')); // "Dec 25, 2023"

Implementation

String format(String format, {String? locale}) {
  return DateFormat(format, locale).format(this);
}