toUTCIso8601StringNoMSUS method

String toUTCIso8601StringNoMSUS()

Dart's DateTime.toIso8601String function includes milliseconds and microseconds. Many backend systems don't support this, and for the most part, time information stored is fine if stored with second accuracy only.

This function returns a String in ISO8601 format, in UTC, down to second accuracy.

Implementation

String toUTCIso8601StringNoMSUS() {
  if (!isUtc) {
    return toUtc().toUTCIso8601StringNoMSUS();
  }

  String y = (year >= -9999 && year <= 9999)
      ? '$year'.padLeft(4, '0')
      : '$year'.padLeft(6, '0');
  String m = '$month'.padLeft(2, '0');
  String d = '$day'.padLeft(2, '0');
  String h = '$hour'.padLeft(2, '0');
  String min = '$minute'.padLeft(2, '0');
  String sec = '$second'.padLeft(2, '0');

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