0

I want to get the time zone of the mobile phone's time.

How can I make it?

I am using Xamarin to implement APP in both android / iOS

What I want is to get such as "GMT+8". Not country name.

4
  • 1
    TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now) Commented Jan 4, 2021 at 14:09
  • @SushiHangover I have tried this. But I got 0 as reponse. Does this need any permission?
    – CC.Wang
    Commented Jan 4, 2021 at 16:03
  • @CC.Wang - "GMT+8" is not a time zone. It is an offset from GMT. Those are very different things. Please read the time zone tag wiki, especially the section titled "Time Zone != Offset". If you are actually looking for an offset, there are several ways to get it - but all will require some point in time as the basis. You can use "now" as the basis, but depending on the time zone, that value might be different at some other date and time. In other words, it's a 1-to-N relationship; a time zone has one or more offsets. Commented Jan 4, 2021 at 18:44
  • 2
    @SushiHangover - Per the docs: "Whenever possible, use the TimeZoneInfo class instead of the TimeZone class." You could do TimeZoneInfo.Local.GetUtcOffset(DateTime.Now), or DateTimeOffset.Now.Offset, or a variety of other approaches, but you should not use the TimeZone class, or you might get inaccurate results in some cases. Commented Jan 4, 2021 at 18:48

1 Answer 1

0

Try this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.