flutter datime withtimzone code example

Example 1: datetimeoffset flutter

// DateTime can only represent local time and UTC time.
// It supports timezone offset for parsing, but normalizes it to UTC

print(DateTime.parse('2018-09-07T17:29:12+02:00').isUtc);
// prints true.

//You can then only convert between local and UTC time using:
// toLocal() or toUtc()

Example 2: flutter date with timezone

/// converts [date] into the following format: `2020-09-16T11:55:01.802248+01:00`
static String formatISOTime(DateTime date) {
  var duration = date.timeZoneOffset;
  if (duration.isNegative)
  	return (date.toIso8601String() + "-${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
  else
  	return (date.toIso8601String() + "+${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}

Tags:

Dart Example