Some times I update the component and post the updated date and the assembly version number so it is recommended to check it once a while.
As all we know .Net’s (up to framework 2) Time Zone class is not much of use since it only gives as objects that are associate with the current time zone, therefore I decided to create a time zone class and called it TimeZoneInfo that can give us information and methods for the other time zones as well. Mainly TimeZoneInfo class uses the windows registry information under the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Time Zones” so it is critical that the registry includes the latest updates from MS.
The class provides all the information and the functionality for the time zones. For example it provides Daylight Saving Time dates for the time zone, Current UtcOffset, Current Time, Current System Time Zone etc.
Using the Class:
vb Code:
'Get all time zones on the system and show them in a ListBox control
'Create new instans of a TimeZoneInfo for a specific time zone.
tzInfo = New TimeZoneInfo("Pacific Standard Time")
'Or
tzInfo = TimeZoneInfo.FromStandardName("Pacific Standard Time")
'Get the current UtcOffset of the time zone
Dim utcOffset As TimeSpan = tzInfo.CurrentUtcOffset
'Get the current time of the time zone
Dim time As DateTime = tzInfo.CurrentTime
'Get a value specifing if it is a dailight saving time currently for the time zone
Dim isDaylight As Boolean = tzInfo.IsDaylightSavingTime
'Get dailight changes of the time zone
Dim dStart, dEnd As DateTime
Dim dt As System.Globalization.DaylightTime
dt = tzInfo.GetDaylightChanges(2007)
dStart = dt.Start
dEnd = dt.End
'And much more
'...
'...
The class should work on 98/windows NT/2000/XP, but I am not sure if it will work on Vista. Please notify me about the results if you tried it on the other windows than XP.
Update History:
09/04/2007 -- Original version.
09/17/2007 -- (Some internal minor changes to optimize the Sort functionality).
09/21/2007 -- (A small bag fix).
09/24/2007 -- (The CurrentTimeZone property of TimeZoneInfo was not getting the correct system current time zone after changing it in the code. This was happening because the .Net TimeZone.CurrentTimeZone has a bug and the TimeZoneInfo class was using this property of TimeZone to return the current system time zone. Now the class implements its own method by using GetTimeZoneInformation Api function which returns the correct system current time zone.
There are also some changes to the IsDaylightSavingTime and GetDaylightChanges functions to optimize and fix some small bugs.)
Please feel free to rate this post , comment and notify me about any problem associated with it.
Last edited by VBDT; Sep 27th, 2007 at 12:10 PM.
Rating is a way of saying thank you. Don't forget to rate always!
In accordance with this CodeBank policy regarding attachments, your Time Zone Information Demo zip file has been edited and all compiled code has been removed.
Please do not include any compiled files in your attachments.
In accordance with this CodeBank policy regarding attachments, your Time Zone Information Demo zip file has been edited and all compiled code has been removed.
Please do not include any compiled files in your attachments.
Thank you.
Hi Hack, I thought it is ok to include compiled file for the people to try the demo of the class if the project is included. If it is a new policy than I think it doesn’t make since to post any demo project at all. I will remove the demo project since there is no reason for it to be posted.
Best regards,
VBDT
Rating is a way of saying thank you. Don't forget to rate always!
It's always been the case.... Why doesn't it make sense to post a demo project, but makes sense to post a compiled demo? Doesn't that defeat the purpose? I want to see how it's used... not just that it can do x and y. Otherwise it's like: Ok, great, it does x and y. But HOW do I call X and Y?
Actually, I posted the entire demo project with its compiled .exe files initially. but, I think I misunderstood Hack my apologies. I see your point now, but I think if the person worries about getting viruses they can simply run the project instead of the exe file. Anyway, I posted the demo project again without the compiled files
Last edited by VBDT; Sep 12th, 2007 at 09:23 PM.
Rating is a way of saying thank you. Don't forget to rate always!
As techgnome stated, this has always been a policy. However, it was an unwritten one until I put the policy in writing and it became an official part of CodeBank posting.
If you will notice the date of the sticky thread which details this policy, you will see that is been officially in place since May of 2005.
The main reason is so that people don't have to worry about getting virus because there aren't any compiled files to get them from.
I have Windows Vista Ultimate and Microsoft Visual Basic 2005 on my pc. I am new in programming and I am very interested with your post regarding Time Zone Class. It seems that i can't run the demo project though. I am currently playing around VB.Net and came accross to this thread. And it seems that you are the only one around the world that posted a thread regarding Time Zones using VB.Net and I am truly fortunate to find it.
Well, I am attempting to create a VB.Net program that would just display the Current Time on the computer with different Time Zone equivalents.
I have Windows Vista Ultimate and Microsoft Visual Basic 2005 on my pc. I am new in programming and I am very interested with your post regarding Time Zone Class. It seems that i can't run the demo project though. I am currently playing around VB.Net and came accross to this thread. And it seems that you are the only one around the world that posted a thread regarding Time Zones using VB.Net and I am truly fortunate to find it.
Well, I am attempting to create a VB.Net program that would just display the Current Time on the computer with different Time Zone equivalents.
To run the demo project you need to download the zip file, unzip it and debag it with VS 2005.
The TimezoneInfo class is designed for up to Win Xp and I am not sure if it will work with vista since ms has changed a lit bit the registry keys of vista, but you can try and see if it works. From there I will see if I can help you.
Rating is a way of saying thank you. Don't forget to rate always!
this is a great post. can any body if i use this class and bind it to a drop down list in vb.net and user makes a selection so how can i pass the selection from dropdown to timezone info class as standard name i am asking this because
if dlst.selectedvalue ="(GMT-5:00) Eastern Time (US & Canada)"
how can you convert it in the constructor of time zone class to "Easter standard time"
if you refer it in
tzinfo = New TimeZoneInfo (Cstr(dlst.selectedValue).Tostring) gives an error that it can not convert a string to timezone info object. would any body know who do i need to change in the timezoneinfo class to have it read the selection in a drop down in standard format or other suggestions are welcome
this is a great post. can any body if i use this class and bind it to a drop down list in vb.net and user makes a selection so how can i pass the selection from dropdown to timezone info class as standard name i am asking this because
if dlst.selectedvalue ="(GMT-5:00) Eastern Time (US & Canada)"
how can you convert it in the constructor of time zone class to "Easter standard time"
if you refer it in
tzinfo = New TimeZoneInfo (Cstr(dlst.selectedValue).Tostring) gives an error that it can not convert a string to timezone info object. would any body know who do i need to change in the timezoneinfo class to have it read the selection in a drop down in standard format or other suggestions are welcome