none
Convert Time zones RRS feed

  • Question

  • Hello! I'm trying to build a Visual Basic form that allows me to convert time zone from a country to another country. For example, the United Kingdom to the United States. I'm looking forward adding all the time zones. 

    So basically I need to enter the date & time of time zone #1 and click on a button that converts it to a textbox or another calendar with time zone #2. That simple, but I cannot find a way to build this form. Please help.

    Edit#1: I'm using Visual Studio Professional 2017.

    Thank you.


    • Edited by Ettra Wednesday, April 11, 2018 2:30 PM
    Wednesday, April 11, 2018 2:23 PM

All replies

  • This can be as simple as subtracting the time zones or more complex depending on the known details.

    To convert to utc you can use date1.ToUniversalTime where date1 is a date data type.

    Do you know the country and time zone already? What do you know and what are you expecting the form to do exactly? ie what will the user input and what result will be output.

    Finally one should consider if detailed adjustments are desired for things like leap years depending on the length of time periods you want to consider.


    Wednesday, April 11, 2018 2:54 PM
  • TZ are stored in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones)
    Wednesday, April 11, 2018 3:12 PM
  • Here is a start but not complete.

    Imports System.Collections.ObjectModel
    Public Class Form1
        ''' <summary>
        ''' one ListBox timeZoneList
        ''' one label lblTimeZone
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo)
            tzCollection = TimeZoneInfo.GetSystemTimeZones()
            timeZoneList.DataSource = tzCollection
        End Sub
        Private Sub timeZoneList_SelectedIndexChanged(sender As Object, e As EventArgs) _
            Handles timeZoneList.SelectedIndexChanged
    
            If timeZoneList.SelectedIndex > 0 Then
                Dim dt As Date = Date.Now
                lblTimeZone.Text = TimeZoneInfo.
                    ConvertTimeBySystemTimeZoneId(
                    dt, TimeZoneInfo.Local.Id, CType(timeZoneList.SelectedItem, TimeZoneInfo).Id).ToString()
            End If
        End Sub
    End Class
    

    The following information (code samples in C#) is worth reading the various topics for a better understanding and what can go wrong.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 11, 2018 4:01 PM
    Moderator
  • I have a compilation of about 300 timezones.  That can be downloaded from here

    https://1drv.ms/u/s!Ai_ivRGbhOmVihUPA4sRvfIaqvlB

    Here are a couple of classes.  The first uses that file to produce  timezone data .  I have not verified every timezones information

    Public Class TimeZones
    
        Private _tz As XElement
        Private _tzs As New List(Of TimeZone)
    
        Public Sub New(path As String)
            Me._tz = XElement.Load(path)
            For Each z As XElement In Me._tz...<Zone>.OrderBy(Function(a) a.<Abbreviation>.Value)
                Dim zone As New TimeZone(z)
                Me._tzs.Add(zone)
            Next
        End Sub
    
        Public Function Abbreviations() As List(Of String)
            Return (From z In Me._tzs Where z.Abbrev <> "" Select z.Abbrev).ToList
        End Function
    
        Public Function Zones() As List(Of TimeZone)
            Return (From z In Me._tzs Where z.Abbrev <> "" Select z).ToList
        End Function
    
        Public Function GetZone(zoneAbbrev As String) As List(Of TimeZone)
            Dim za As String = zoneAbbrev.ToUpper
            Return (From z In Me._tzs Where z.Abbrev = za Select z).ToList
        End Function
    End Class
    
    Public Class TimeZone
        Public Abbrev As String = ""
        Public Comment As String = ""
        Public Name As String
        Public Location As String
        Public Mil As String
        Public GMTOffs As TimeSpan
    
        '   <Zone>
        '     <Abbreviation>A</Abbreviation>
        '     <FullName>Alpha Time Zone</FullName>
        '     <Location>Military</Location>
        '     <GMToffset>01:00</GMToffset>
        '     <MilitaryTZ>A</MilitaryTZ>
        '   </Zone>
        Public Sub New(zone As XElement)
            If zone.<Abbreviation>.Value.Contains("*") Then
                Dim p() As String = zone.<Abbreviation>.Value.Split("*"c)
                Me.Abbrev = p(0).Trim
                Me.Comment = p(1).Trim
            Else
                Me.Abbrev = zone.<Abbreviation>.Value.Trim
            End If
            Me.Name = zone.<FullName>.Value
            Me.Location = zone.<Location>.Value
            Me.GMTOffs = TimeSpan.Parse(zone.<GMToffset>.Value)
            If Math.Abs(Me.GMTOffs.TotalHours) >= 23 Then Stop
            Me.Mil = zone.<MilitaryTZ>.Value
        End Sub
    End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Wednesday, April 11, 2018 4:32 PM
  • Thank you so much for your reply!

    • The user chooses a state from the 50 states of America.

    • Chooses the date and time and click on the button to convert it.

    • Form converts that state time to GMT +2.

    • Would you please help me with the code?

    Go 

    Because I'm not verified yet, I cannot post pictures or links.

    So, to GYAZO.com, and type after it /96515242d67ab27662043a7f7546c487. You'll find a picture of what I am trying to do.

    Kind Regards,



    • Edited by Ettra Wednesday, April 11, 2018 4:38 PM
    Wednesday, April 11, 2018 4:36 PM
  • Can you please tell me more about it?

    I open a new file in Visual Studio Professional 2017, and choose VB then load a new Windows Form App? What are the tools that I use for this?

    Wednesday, April 11, 2018 4:41 PM
  • Also, can someone give a code for the button to convert the time zone?
    Wednesday, April 11, 2018 4:44 PM
  • Also, can someone give a code for the button to convert the time zone?

    Here is an example conversion. I added 2 hours which is moving east?? of Greenwich. You should test it.

    Add a datetimepicker, button, and label control to an empty form and then paste this code.

    Public Class Form3
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DateTimePicker1.Format = DateTimePickerFormat.Time
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim date1 As Date = DateTimePicker1.Value
    
            Label1.Text =
                "Location: " & System.TimeZoneInfo.Local.ToString & vbLf & vbLf &
                "Daylight: " & Now.IsDaylightSavingTime & vbLf & vbLf &
                date1.ToString & vbLf & vbLf &
                "GMT + 2 = " & date1.ToUniversalTime.AddHours(2).ToShortTimeString
    
        End Sub
    End Class


    Wednesday, April 11, 2018 5:53 PM
  • Hi Ettra,

    I add one Combobox control, one DateTimePicker and one TextBox control in the Form, do one example that you can refer to:

    Public Class Frmtimezone
        Dim tzi As ReadOnlyCollection(Of TimeZoneInfo)
    
        Private Sub Frmtimezone_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            tzi = TimeZoneInfo.GetSystemTimeZones()
    
            ComboBox1.DataSource = GettimeItems()
            ComboBox1.DisplayMember = "_displayName"
            ComboBox1.ValueMember = "_id"
        End Sub
        Private Function GettimeItems() As List(Of ListItem)
    
            Dim timeItems = New List(Of ListItem)
            For Each timeZone As TimeZoneInfo In tzi
                timeItems.Add(New ListItem(timeZone.DisplayName, timeZone.Id))
            Next
            Return timeItems
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex > 0 Then
                Dim dt As Date = DateTimePicker1.Value
    
                TextBox1.Text = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, ComboBox1.SelectedValue).ToString()
            End If
        End Sub
    End Class
    Public Class ListItem
        Private displayName As String
        Private id As String
        Public Property _displayName() As String
            Get
                Return displayName
            End Get
            Set(value As String)
                displayName = value
            End Set
        End Property
    
        Public Property _id() As String
            Get
                Return id
            End Get
            Set(value As String)
                id = value
            End Set
        End Property
    
        Public Sub New(displayName As String, id As String)
            Me.displayName = displayName
            Me.id = id
        End Sub
    End Class

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, April 19, 2018 9:33 AM
    Thursday, April 12, 2018 6:31 AM
    Moderator
  • Also, can someone give a code for the button to convert the time zone?
    Can you not better write: "Can somebody give me the code for the whole program including a way to obfuscate it and to protect it with a security key so nobody can steal it from me when I sell it." 

    Success
    Cor

    Thursday, April 12, 2018 11:47 AM
  • Also, can someone give a code for the button to convert the time zone?

    Can you not better write: "Can somebody give me the code for the whole program including a way to obfuscate it and to protect it with a security key so nobody can steal it from me when I sell it." 

    Success
    Cor

    I'm a Graphics Designer and I am trying to make that simple program with my small knowledge around Visual Studio especially Visual Basic to moderate my projects time and other things. I'm working on this simple program for two weeks now and learning more in the process. Thank you for the reply.
    Thursday, April 12, 2018 4:36 PM
  • Thank you so much for your reply!

    • The user chooses a state from the 50 states of America.

    • Chooses the date and time and click on the button to convert it.

    • Form converts that state time to GMT +2.

    • Would you please help me with the code?

    Go 

    Because I'm not verified yet, I cannot post pictures or links.

    So, to GYAZO.com, and type after it /96515242d67ab27662043a7f7546c487. You'll find a picture of what I am trying to do.

    Kind Regards,



    Sounds like homework. If you're trying to make a grade then try doing some work on your own please.

    Plus most states use daylight savings time at some point.


    La vida loca

    Thursday, April 12, 2018 4:56 PM

  • I'm a Graphics Designer and I am trying to make that simple program with my small knowledge around Visual Studio especially Visual Basic to moderate my projects time and other things. I'm working on this simple program for two weeks now and learning more in the process. Thank you for the reply.

    There is not one line of code in this thread which shows you're busy with that only many times:

    "Give me the code"


    Success
    Cor

    Thursday, April 12, 2018 5:31 PM
  • Hi Ettra,

    If you have solved your issue, please remember to close your thread by marking the helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 19, 2018 9:18 AM
    Moderator