How to get current timezone region (tz database name) in Flutter
Solution 1
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
Solution 2
There is a great library for this: https://pub.dev/packages/flutter_native_timezone
Solution 3
/// 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
Solution 4
https://pub.dartlang.org/packages/timezone with https://docs.flutter.io/flutter/dart-core/DateTime/timeZoneOffset.html might work for you.
You can always make native functionality available to Flutter by building an https://flutter.io/docs/development/packages-and-plugins/developing-packages#developing-plugin-packages
https://github.com/flutter/flutter/issues/12531 is also somewhat related.
Related videos on Youtube
Junaid Durrani
Updated on March 01, 2022Comments
-
Junaid Durrani about 2 years
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.
Can anyone guide me, how can i get the current timezone in flutter using dart.
-
anmol.majhail over 5 yearsFor name you can use -
DateTime.now().timeZoneName
-
Junaid Durrani over 5 yearsI don't want this format, my api takes Europe/london timezone's format. but this code give me The Europe Standard time
-
tim-montague about 2 yearsDO NOT USE
DateTime.now().timeZoneName
! Timezone abbreviations are ambiguous. CST for example can mean Central Standard Time, Cuba Standard Time, or China Standard Time. There's a wiki page that explains more (en.wikipedia.org/wiki/List_of_time_zone_abbreviations). It's not ISO 8601 compliant, and mostly deprecated for modern use. Confused as to why the Dart team makes these timezones more accessible than the tz timezone regions. -
tim-montague about 2 years@MehulRanpara - While probably not an ideal solution, if you use the Windows operating system,
DateTime.now().timeZoneName
should return the full time zone name (api.dart.dev/stable/2.16.1/dart-core/DateTime/timeZoneName.html).
-
-
Junaid Durrani over 5 yearsi do not understand and little bit confuse about how to get the timezone. Can You plz give me the snippet of getting current timezone ? @Gunter Zochbauer
-
Günter Zöchbauer over 5 yearsThe package docs should explain it. I don't have a concrete snippet.
-
Harsh Pipaliya about 3 yearsdateTime.timeZoneName returns only "+5:30" not IST
-
Benjámin Gerván about 3 yearsWhat if the offset 1:30, wouldn't be a bug in the above code then?
-
omer about 3 yearsJust Checked: It returns Abbreviated String.
DateTime.now().timeZoneName
-
DNS over 2 yearsdateTime timeZoneName returns +0430 for me, I can't get the abbreviation
-
omer over 2 years@DNS go to darpad.dev and try this code
void main() { print(DateTime.now().timeZoneName); }
-
Syscall over 2 yearsYour answer could be improved with additional supporting information. Please edit your answer to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
tim-montague about 2 yearsOP wants a solution that gives the tz timezone region, so
DateTime.now().timeZoneName
andDateTime.now().timeZoneOffset
are not solutions. -
tim-montague about 2 yearsThe
timeZoneName
(which gives an abbreviation on all systems except Windows) andtimeZoneOffset
when combined might be enough to answer the OPs question. But one would need to build a map, that uses thetimeZoneName
andtimeZoneOffset
to pick the correct tz timezone region.