Help with time zone conversions

chopps

New member
Joined
Feb 13, 2011
Messages
4
Programming Experience
Beginner
Hello everyone! I have trying to complete an assignment where I have to convert a string from user input to all different US time zones and then display them. At first I tried doing this just by using math but it seemed extremely inefficient and messy so I was told to use the DateTime function which seems easier with the exception of one thing - I have found absolutely no examples of converting time to different time zones. I was able to take the string and convert it to UTC time thinking it would make it easier but now I don't know how to convert them to PST, EST, MST, or CST. I included the function below:

****************************
Private Sub timeDifference()
Dim meridiem As String
Dim ante As String = " AM"
Dim post As String = " PM"
Dim time As String = TextBox1.Text
Dim timeString As String = time & meridiem
Dim curTime As DateTime = CDate(timeString).ToUniversalTime
Label1.Text = curTime.ToShortTimeString
End Sub
****************************

Any help on this would be much appreciated.
 
DateTime is not a function. It is a type. It contains a value that represents a point in time, defined by the date and the time.

I'm not sure what the requirements of your assignment are (maybe you have to use straight maths) but, in a real application, you would use the TimeZoneInfo class to convert between time zones.

First, you should use the DateTime.TryParse or .TryParseExact method to convert the user-entered text into a DateTime value. That will perform the conversion using a standard or custom format and not throw an exception if the data is invalid. Note that there's no need to convert the date/time to UTC unless you specifically need UTC. The TimeZoneInfo class will assume the local system time zone unless otherwise specified.

Next, you call GetSystemTimeZones on the TimeZone class and then pick out the ones you want from the list by name, code or whatever. You can then loop through that smaller list and call ConvertTime on the TimeZoneInfo class to convert your local date/time to the date/time in that time zone. The TimeZoneInfo class will also take daylight saving into account.
 
Back
Top