How to get timezone offset in minutes on WP7?

I need to know what a temporary offset is for a device that launches my application in minutes. For example, for Pacific standard time (where I'm testing) I need to get int -480. Examples (which I could not get from my current location, but to give you an idea of ​​what I need) in Afghanistan, I would get 270, and in the UK I would get 0.

My best attempts so far have all returned 0, which is the wrong value:

int timezoneOffsetInMinutes = (DateTime.Now.ToLocalTime() - DateTime.Now).Minutes// returns 0 instead of the expected -480 

and

 int timezoneOffsetInMinutes = TimeZoneInfo.Utc.BaseUtcOffset.Minutes - TimeZoneInfo.Local.BaseUtcOffset.Minutes;// also returns 0, not -480 

I was not sure that this post asked the same question (since no examples of offsets or sample code are given), but the one answer that is given there is definitely not related to what I'm looking for.

+6
source share
2 answers

If you need a basic / standard offset, then TimeZoneInfo is probably the most correct way to access this information (and also gives you access to other information, for example, does the time zone support daylight saving time):

 TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset; 

However, if you want the current offset (including DST changes), I would be used to using DateTimeOffset , since it has basically a completed DateTime (it can represent "local" time for a time zone that is different from the local computer):

 TimeSpan offset = DateTimeOffset.Now.Offet; 
+5
source
 DateTime utc = DateTime.Now.ToUniversalTime(); DateTime local = DateTime.Now; TimeSpan diff = (utc - local); 

This returns the correct time difference between you and UTC.

+2
source

Source: https://habr.com/ru/post/892918/


All Articles