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

32,690

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
Share:
32,690

Related videos on Youtube

Junaid Durrani
Author by

Junaid Durrani

Updated on March 01, 2022

Comments

  • Junaid Durrani
    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.

    Flutter Timezone (as ZoneId)

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

    • anmol.majhail
      anmol.majhail over 5 years
      For name you can use - DateTime.now().timeZoneName
    • Junaid Durrani
      Junaid Durrani over 5 years
      I don't want this format, my api takes Europe/london timezone's format. but this code give me The Europe Standard time
    • tim-montague
      tim-montague about 2 years
      DO 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
      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
    Junaid Durrani over 5 years
    i 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
    Günter Zöchbauer over 5 years
    The package docs should explain it. I don't have a concrete snippet.
  • Harsh Pipaliya
    Harsh Pipaliya about 3 years
    dateTime.timeZoneName returns only "+5:30" not IST
  • Benjámin Gerván
    Benjámin Gerván about 3 years
    What if the offset 1:30, wouldn't be a bug in the above code then?
  • omer
    omer about 3 years
    Just Checked: It returns Abbreviated String. DateTime.now().timeZoneName
  • DNS
    DNS over 2 years
    dateTime timeZoneName returns +0430 for me, I can't get the abbreviation
  • omer
    omer over 2 years
    @DNS go to darpad.dev and try this code void main() { print(DateTime.now().timeZoneName); }
  • Syscall
    Syscall over 2 years
    Your 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
    tim-montague about 2 years
    OP wants a solution that gives the tz timezone region, so DateTime.now().timeZoneName and DateTime.now().timeZoneOffset are not solutions.
  • tim-montague
    tim-montague about 2 years
    The timeZoneName (which gives an abbreviation on all systems except Windows) and timeZoneOffset when combined might be enough to answer the OPs question. But one would need to build a map, that uses the timeZoneName and timeZoneOffset to pick the correct tz timezone region.