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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With