toIso8601String method

String toIso8601String()

Returns an ISO-8601 full-precision extended format representation.

The format is yyyy-MM-ddTHH:mm:sszzzzzz where:

  • yyyy is a, possibly negative, four digit representation of the year, if the year is in the range -9999 to 9999, otherwise it is a signed six digit representation of the year.
  • MM is the month in the range 01 to 12,
  • dd is the day of the month in the range 01 to 31,
  • HH are hours in the range 00 to 23,
  • mm are minutes in the range 00 to 59,
  • ss are seconds in the range 00 to 59 (no leap seconds),
  • zzzzzz is the time zone (Z if UTC, otherwise +HH:mm or -HH:mm),

Implementation

String toIso8601String() {
  final y = (year >= -9999 && year <= 9999) ? _fourDigits(year) : _sixDigits(year);
  final m = _twoDigits(month);
  final d = _twoDigits(day);
  final h = _twoDigits(hour);
  final min = _twoDigits(minute);
  final sec = _twoDigits(second);

  if (timeZoneOffset == 0) {
    return '$y-$m-${d}T$h:$min:${sec}Z';
  } else {
    final strTimeZoneOffset = _timeZoneOffsetAsString();

    return '$y-$m-${d}T$h:$min:$sec$strTimeZoneOffset';
  }
}