toIso8601StringWithOffset method

String toIso8601StringWithOffset()

Returns the DateTime object as an ISO 8601 string with timezone offset. If the DateTime is in UTC, it appends "Z" to the string.

Example:

DateTime.now().toIso8601StringWithOffset(); // "2024-08-07T12:34:56.789+07:00" or "2024-08-07T12:34:56.789Z" if UTC

Implementation

String toIso8601StringWithOffset() {
  String iso8601String = toIso8601String();
  String offsetString = getTimezoneOffsetString();
  return iso8601String + offsetString;
}