getTimezoneOffsetString method
Returns the timezone offset as a string in the format ±hh:mm.
For UTC dates, returns an empty string (not "Z"). For local dates,
returns the offset in the format +HH:MM or -HH:MM (e.g., +07:00,
-05:00).
Returns: A string representation of the timezone offset, or an empty string for UTC.
Example:
final localTime = DateTime.now();
print(localTime.getTimezoneOffsetString()); // "+07:00" or "-05:00"
final utcTime = DateTime.now().toUtc();
print(utcTime.getTimezoneOffsetString()); // "" (empty for 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")}";
}
}