timeZoneOffSet method
Returns the formatted timezone offset as a string in ±HH:mm
format.
The method retrieves the current timezone offset from UTC and formats it properly with leading zeros for hours and minutes.
Example Usage:
final date = DateTime.now();
print(date.timeZoneOffsetFormatted()); // Example Output: "+05:30"
Returns:
- A string representing the timezone offset in
±HH:mm
format.
How It Works:
- Retrieves the timezone offset from the
DateTime
object. - Extracts hours and minutes, ensuring they have leading zeros.
- Handles negative offsets correctly by prefixing
-
when needed.
Note:
- This method assumes
addZeroPrefix()
is an extension onint
that ensures numbers are at least two digits (e.g.,5
→"05"
).
Implementation
String timeZoneOffSet() {
final offset = this.timeZoneOffset;
final hours = offset.inHours.addZeroPrefix();
final minutes = (offset.inMinutes % 60).addZeroPrefix();
return "${offset.isNegative ? '-' : '+'}$hours:$minutes";
}