如何在颤振中获取当前时区

Jun*_*ani 22 timezone flutter

我是新手,我的休息 api 将当前时区设为欧洲/伦敦。我不知道在颤动中获取当前时区。stackoverflow 中有一个关于此主题的问题,但没有用户给出答案。

颤振时区(作为 ZoneId)

任何人都可以指导我,我如何使用 dart 获取当前时区的抖动。

Ome*_* JR 14

/// 时区名称。/// /// 该值由操作系统提供,可以是 /// 缩写或全名。
/// /// 在浏览器或类 Unix 系统上通常返回缩写, /// 例如“CET”或“CEST”。在 Windows 上返回全名,例如 ///“太平洋标准时间”。
external String get timeZoneName;

/// 时区偏移量, /// 是本地时间与 UTC 之间的差异。/// /// 对于 UTC 以东的时区,偏移量为正。/// /// 注意,JavaScript、Python 和 C 返回 UTC 和 /// 本地时间之间的差异。Java、C# 和 Ruby 返回本地时间和 /// UTC 之间的差异。
external Duration get timeZoneOffset;

DateTime.now().timeZoneName

如果时间实例采用 UTC。

timeInstance.toLocal().timeZoneName

而且,

/// DateTime 类不提供国际化。/// 要国际化您的代码,请使用 /// intl包。

或者

要获取时区Europe/Moscow格式:从这里:使用flutter_native_timezone

final String currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
print(currentTimeZone); // Europe/Moscow
Run Code Online (Sandbox Code Playgroud)


jit*_*555 13

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);
Run Code Online (Sandbox Code Playgroud)

Output:

1. India

timeZoneName: IST
timeZoneOffset: 5:30:00.000000
Run Code Online (Sandbox Code Playgroud)

2. America/Los_Angeles

timeZoneName: PST
timeZoneOffset: -8:00:00.000000
Run Code Online (Sandbox Code Playgroud)

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

  • dateTime.timeZoneName 仅返回“+5:30”而不是 IST (4认同)