yyyy property

String get yyyy

year number string ensured to have length of 4

date.year should be between 0 and 9999 or exception will be thrown

Implementation

String get yyyy {
  final int year = date.year;

  if (year < 0) {
    throw StateError('date.year = $year < 0');
  }

  if (year > 9999) {
    throw StateError('date.year = $year > 9999');
  }

  final String str = year.toString();

  switch (str.length) {
    case 4:
      return str;
    case 3:
      return '0$str';
    case 2:
      return '00$str';
    case 1:
      return '000$str';
    default: // case: 0
      return '0000';
  }
}