Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the timezone offset in minutes on WP7?

I need to be able to know what the timezone offset is for the device running my app in minutes. For example, for Pacific standard time (where I am testing), I need to get the int -480. Examples (which I should not be able to get from my current location - but to give you an idea of what I need) in Afghanistan I would get 270 and the UK is where I would get 0.

My best attempts so far have all returned 0 instead, 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 wasn't sure if this post was asking the same question (as no example offsets or example code is given), but the one answer that is given there is definitely unrelated to what I'm looking for.

like image 580
Subcreation Avatar asked Jul 17 '11 03:07

Subcreation


1 Answers

If you want the base/standard offset, then TimeZoneInfo is probably the most correct way of accessing that information (and also gives you access to other information, such as whether the timezone supports daylight savings time):

TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset;

However, if you want the current offset (including DST changes), I'd get into the habit of using DateTimeOffset, as it has basically superceded DateTime (it's able represent a "local" time to a timezone that is different to the local machine's):

TimeSpan offset = DateTimeOffset.Now.Offet;
like image 99
Richard Szalay Avatar answered Oct 24 '22 23:10

Richard Szalay