getTimezoneOffsetString method

String getTimezoneOffsetString()

Returns the timezone offset as a string in the format ±hh:mm. If the DateTime is in UTC, it returns "Z".

Example:

DateTime.now().getTimezoneOffsetString(); // "+07:00" or "Z" if UTC

Implementation

String getTimezoneOffsetString() {
  if (isUtc) {
    return "";
  } else {
    Duration offset = timeZoneOffset;
    String sign = offset.isNegative ? "-" : "+";
    int hours = offset.inHours.abs();
    int minutes = offset.inMinutes.remainder(60).abs();
    return "$sign${hours.toString().padLeft(2, "0")}:${minutes.toString().padLeft(2, "0")}";
  }
}