How to get current timezone region (tz database name) in Flutter

Viewed 38820

I am new to flutter and my rest api takes current timezone as Europe/london. I don't know to get current timezone in flutter. There is a question in stackoverflow about this topic but no user gave the answer.

Flutter Timezone (as ZoneId)

Can anyone guide me, how can i get the current timezone in flutter using dart.

8 Answers

DateTime is the default class give use timezone and time offset both required to solve timezone related issue.

 DateTime dateTime = DateTime.now();
 print(dateTime.timeZoneName);
 print(dateTime.timeZoneOffset);

Output:

1. India

timeZoneName: IST
timeZoneOffset: 5:30:00.000000

2. America/Los_Angeles

timeZoneName: PST
timeZoneOffset: -8:00:00.000000

timezone list: https://help.syncfusion.com/flutter/calendar/timezone

/// The time zone name. /// /// This value is provided by the operating system and may be an /// abbreviation or a full name.
/// /// In the browser or on Unix-like systems commonly returns abbreviations, /// such as "CET" or "CEST". On Windows returns the full name, for example /// "Pacific Standard Time".
external String get timeZoneName;

/// The time zone offset, which /// is the difference between local time and UTC. /// /// The offset is positive for time zones east of UTC. /// /// Note, that JavaScript, Python and C return the difference between UTC and /// local time. Java, C# and Ruby return the difference between local time and /// UTC.
external Duration get timeZoneOffset;

DateTime.now().timeZoneName

If time instance is in UTC.

timeInstance.toLocal().timeZoneName

Moreover,

/// The DateTime class does not provide internationalization. /// To internationalize your code, use /// the intl package.

OR

To get timezone in Europe/Moscow format: From here: Using flutter_native_timezone

final String currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
print(currentTimeZone); // Europe/Moscow
  var date = DateTime.now().toIso8601String().split(".")[0];
  if(DateTime.now().timeZoneOffset.isNegative){
    date += "-";
  } else {
    date += "+";
  }
  final timeZoneSplit = DateTime.now().timeZoneOffset.toString().split(":");

  var hour = int.parse(timeZoneSplit[0]);
  if(hour < 10){
    date += "0${timeZoneSplit[0]}";
  }
  date += ":" + timeZoneSplit[1];
  print(date);

This will work and this will give something like 2021-07-15T19:28:25+05:30. Here 5:30 is IST. Which should be good.

import 'package:intl/intl.dart';

DateTime startDate = DateTime.now().toLocal();
var date = DateFormat.yMMMd().format(startDate);

Dart does not provide a tz timezone for the platform directly, however one can use the following two properties and a mapping to determine the tz timezone.


(1) The DateTime.now().timeZoneName property gives a timezone abbreviation (on Windows it gives a full name). This abbreviation alone is not enough to determine a time zone. For example, CST can mean Central Standard Time, Cuba Standard Time, China Standard Time, etc.

(2) The DateTime.now().timeZoneOffset property gives a UTC offset, which vertical spans multiple timezones. This alone is not enough to determine a timezone.


To determine the tz timezone, use both the DateTime.now().timeZoneName and DateTime.now().timeZoneOffset properties, and map to the appropriate tz timezone.

Example below (which could be created as a Flutter package):

String timeZone = DateTime.now().timeZoneName.toString();
String timeZoneOffset = DateTime.now().timeZoneOffset.toString();

String tzTimeZoneName = '';
String timeZoneName = '';

if (
  (timeZone == 'CST' || timeZone == 'CST ( Central Standard Time )') &&
  timeZoneOffset == '-6:00:00.00-0.0'
) {
  tzTimeZoneName = 'America/Chicago';
  timeZoneName = 'Central Standard Time';
}

These two pages can be used to create a full mapping: https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Note: This mapping technique can also be used to convert the timezone abbreviation into the full name timezone (on non-Window systems). For example CST, to "Central Standard Time". I also think the Dart team, could have patched this on non-Window systems, as they have both the timezone abbreviation and the UTC offset. ‍♂️

Maybe useful for someone, I'm using:

String formatDateToStringWithTimeZone(DateTime date) {
  var dur = date.timeZoneOffset;
  if (dur.isNegative)
    return "${DateFormat("y-MM-ddTHH:mm:ss").format(date)}-${dur.inHours.toString().padLeft(2, '0')}:${(dur.inMinutes - (dur.inHours * 60)).toString().padLeft(2, '0')}";
  else
    return "${DateFormat("y-MM-ddTHH:mm:ss").format(date)}+${dur.inHours.toString().padLeft(2, '0')}:${(dur.inMinutes - (dur.inHours * 60)).toString().padLeft(2, '0')}";
}
Related