Solved

Getting UTC OFFSET OF a timezone

Posted on 2010-09-02
9
495 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:hainansyndrome
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33591618
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

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33591692
You can also read this ...there is a good example ...
http://msdn.microsoft.com/en-us/library/system.timezone.getutcoffset(VS.71).aspx
0
 

Author Comment

by:hainansyndrome
ID: 33591699
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
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33591794
you can use for any other pc this:
Dim OtherPcOffset As TimeSpan TimeZoneInfo.Local.GetUtcOffset(DateTimeOffset.Now)
REad this article ...
http://msdn.microsoft.com/en-us/library/bb397769.aspx
about Converting DateTimeOffset Values
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:hainansyndrome
ID: 33595825
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
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33595961
0
 

Author Comment

by:hainansyndrome
ID: 33607890
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
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33608027
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
http://www.michaelbrumm.com/simpletimezone.html
0
 

Author Closing Comment

by:hainansyndrome
ID: 33718866
took some digging around
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now