Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 542 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get current timezone region (tz database name) in Flutter

#1
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.

[To see links please register here]


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

#2

[To see links please register here]

with

[To see links please register here]

might work for you.

You can always make native functionality available to Flutter by building an

[To see links please register here]


[To see links please register here]

is also somewhat related.
Reply

#3
There is a great library for this:

[To see links please register here]

Reply

#4
`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:

[To see links please register here]

Reply

#5
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')}";
}
Reply

#6
> /// 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](

[To see links please register here]

) package.

OR

To get timezone in `Europe/Moscow` format:
From [here][1]: Using [flutter_native_timezone](

[To see links please register here]

)

final String currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
print(currentTimeZone); // Europe/Moscow


[1]:

[To see links please register here]

Reply

#7
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.
Reply

#8
import 'package:intl/intl.dart';

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

#9
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):

```dart
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:

[To see links please register here]

[To see links please register here]


> **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. 🤷🏻‍♂️
Reply

#10
// Use this

Map timezoneNames = {
0: 'UTC',
10800000: 'Indian/Mayotte',
3600000: 'Europe/London',
7200000: 'Europe/Zurich',
-32400000: 'Pacific/Gambier',
-28800000: 'US/Alaska',
-14400000: 'US/Eastern',
-10800000: 'Canada/Atlantic',
-18000000: 'US/Central',
-21600000: 'US/Mountain',
-25200000: 'US/Pacific',
-7200000: 'Atlantic/South_Georgia',
-9000000: 'Canada/Newfoundland',
39600000: 'Pacific/Pohnpei',
25200000: 'Indian/Christmas',
36000000: 'Pacific/Saipan',
18000000: 'Indian/Maldives',
46800000: 'Pacific/Tongatapu',
21600000: 'Indian/Chagos',
43200000: 'Pacific/Wallis',
14400000: 'Indian/Reunion',
28800000: 'Australia/Perth',
32400000: 'Pacific/Palau',
19800000: 'Asia/Kolkata',
16200000: 'Asia/Kabul',
20700000: 'Asia/Kathmandu',
23400000: 'Indian/Cocos',
12600000: 'Asia/Tehran',
-3600000: 'Atlantic/Cape_Verde',
37800000: 'Australia/Broken_Hill',
34200000: 'Australia/Darwin',
31500000: 'Australia/Eucla',
49500000: 'Pacific/Chatham',
-36000000: 'US/Hawaii',
50400000: 'Pacific/Kiritimati',
-34200000: 'Pacific/Marquesas',
-39600000: 'Pacific/Pago_Pago'
};

data[DateTime.now().timeZoneOffset.inMilliseconds] // Ex : Indian/Mayotte
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through