formatDateTimeToIsoWithTimezone method

String formatDateTimeToIsoWithTimezone()

Formats the DateTime as ISO with timezone: yyyy-MM-ddTHH:mm:ss+HHMM Works without intl package.

Example:

DateTime.utc(2023, 5, 10, 14, 30).formatDateTimeToIsoWithTimezone();
// "2023-05-10T14:30:00+0000"

DateTime(2023, 5, 10, 14, 30).formatDateTimeToIsoWithTimezone();
// "2023-05-10T14:30:00+0200"

Implementation

String formatDateTimeToIsoWithTimezone() {
  String twoDigits(int n) => n.toString().padLeft(2, '0');

  final y = year;
  final m = twoDigits(month);
  final d = twoDigits(day);
  final h = twoDigits(hour);
  final min = twoDigits(minute);
  final s = twoDigits(second);

  return '$y-$m-${d}T$h:$min:$s';
}