toDateString method

String toDateString()

This date as a simple "year-month-day" string.

If the year is negative, it starts with a minus sign. The year is padded to at least four digits, the month and day to two digits.

Implementation

String toDateString() {
  final year = this.year;
  final month = this.month;
  final day = this.day;
  String yearString;
  if (year.abs() < 1000) {
    yearString = year.abs().toString().padLeft(4, '0');
    if (year < 0) yearString = '-$yearString';
  } else {
    yearString = year.toString();
  }
  return "$yearString-${month < 10 ? "0" : ""}$month-${day < 10 ? "0" : ""}$day";
}