Getting UTC OFFSET OF a timezone

I need a way on a clients pc to query what that pc believes is the UTC offset of any timezone so I can add or subtract it from the UTC of their timezone to get the offset in hours from the particular timezone

example: running code on a pc in eastern timezone i need to get the UTC
i want to pass in the timezone for Mountain and then add or subtract that value form the eastern timezone to get teh number of hours diffence
Dim tzTimeZone As TimeZone = System.TimeZone.CurrentTimeZone
        Dim tsUTCoffset As Integer = tzTimeZone.GetUtcOffset(Now()).Hours

Open in new window

Who is Participating?
John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
You can use a combination of all above...
You can always detect when daylight saving is applied on a client using this:
Dim t As TimeZone = System.TimeZone.CurrentTimeZone
Dim dls as boolean = t.IsDaylightSavingTime(Now)

PS: pc clock will auto update daylightsaving if needed

Also take a look here
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
try this....
DateTime.Now will return the "current" time based on your time zone.

DateTime.ToUniversalTime will return GMT time.

You can subtract the two values to get the difference...

Dim now As DateTime = DateTime.Now
Dim nowGMT As DateTime = now.ToUniversalTime()
Dim diff As TimeSpan = now.Subtract(nowGMT)
Debug.WriteLine(diff, "diff")

Open in new window

John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
You can also read this ...there is a good example ...
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

hainansyndromeAuthor Commented:
no i need to get the difference of 2 timezones

1 being the zone of the pc this code is running off of
and the other being a random zone
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
you can use for any other pc this:
Dim OtherPcOffset As TimeSpan TimeZoneInfo.Local.GetUtcOffset(DateTimeOffset.Now)
REad this article ...
about Converting DateTimeOffset Values
hainansyndromeAuthor Commented:
ok hmmmmmmmm maybe this makes more sense

we are pulling data from a webservice to a PC
this pc could be in any timeszone, I have no trouble getting the timezone information for that timezone

the data from the webservice is a date time, so it gets offset to that timezone

SInce DST changed a while back i cant hard code Central TIme Zones offset
I need to get Central Time's offset per that PC to then add or substract from that Timeezones so I know how many hours i need to add or subtract from my date time

perviously we were just adding 12 hours to the datetime but i want it right
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
hainansyndromeAuthor Commented:
i read it and the problem i come up with is that central time is not always GMT -6 it is sometimes -5
it all hangs on DST just need a function that can grab all the settings from the local machine
anything short of changing the timezone to get the settings
hainansyndromeAuthor Commented:
took some digging around
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.