format static method

  1. @useResult
String format(
  1. int year, [
  2. int month = 1,
  3. int day = 1
])

Formats the date as a ISO-8601 date.

print(Dates.format(2023, 4, 1)); // '2023-04-01'

Implementation

@useResult static String format(int year, [int month = 1, int day = 1]) {
  final sign = year < 0 ? '-' : '';
  final yyyy = year.abs().toString().padLeft(4, '0');
  final mm = month.toString().padLeft(2, '0');
  final dd = day.toString().padLeft(2, '0');

  return '$sign$yyyy-$mm-$dd';
}