【问题标题】:How to get time zone using xamarin in android & iOS如何在 Android 和 iOS 中使用 xamarin 获取时区
【发布时间】:2021-01-04 13:48:58
【问题描述】:

我想获取手机时间的时区。

我该怎么做?

我正在使用 Xamarin 在 android / iOS 中实现 APP

我想要的是得到诸如“GMT+8”之类的东西。不是国家名称。

【问题讨论】:

  • TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
  • @SushiHangover 我试过了。但我得到 0 作为回应。这需要任何许可吗?
  • @CC.Wang - "GMT+8" 不是时区。它是 GMT 的偏移量。这些是非常不同的事情。请阅读the time zone tag wiki,尤其是标题为"Time Zone != Offset" 的部分。如果您实际上是在寻找一个偏移量,有几种方法可以得到它——但都需要某个时间点作为基础。您可以使用“现在”作为基础,但根据时区,该值可能在其他日期和时间有所不同。换句话说,这是一对 N 的关系; 时区有一个或多个偏移量
  • @SushiHangover - Per the docs“尽可能使用 TimeZoneInfo 类而不是 TimeZone 类。” 你可以使用 TimeZoneInfo.Local.GetUtcOffset(DateTime.Now)DateTimeOffset.Now.Offset,或各种其他方法,但您不应使用 TimeZone 类,否则在某些情况下您可能会得到不准确的结果。

标签: c# xamarin xamarin.android xamarin.ios timezone


【解决方案1】:

试试这个:

TimeSpan ts = new DateTimeOffset(DateTime.Now).Offset;

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2021-08-30
    • 2016-02-22
    相关资源
    最近更新 更多