Getting Local Time Using Zip Code in Visual Basic 2008

Today, I will teach you how to get the local time by using a zip code in Visual Basic 2008. If you want to find out the time in any countries around the world, all you have to do is to type the zip code of the country that you desire and then its local time will automatically appear. In here, I use the web service proxy to get the local time of a certain zip code. Let’s begin: Open the Visual Basic 2008, create a new Windows Application and do the Form just like this. first form After that, go the solution explorer, right click and hit “Add Service references”. second form Then, right click the folder of “Service references” and hit again the “Add Service references”. Add this “http://www.ripedev.com/webservices/LocalTime.asmx” for the address and type “local_time” in the Namespace. After that, hit ok. third form Then, you will have now your “Service References”. forth form Double click the “Get Time” Button and do this following code for getting the local time.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. 'DECLARE THE VARIABLE AND CREATE AN INSTANCE OF THE WEB SERVICE PROXY CLASS
  4. Dim localtime As New local_time.LocalTimeSoapClient
  5. Dim formattime As Date
  6. Dim addtime As Integer
  7.  
  8. 'DISPLAY THE PROPER CURSOR
  9. Cursor = Cursors.WaitCursor
  10. 'PROCESSES ALL THE WINDOWS MESSAGE THAT IS CURRENTLY IN THE MESSAGE QUEUE.
  11. Application.DoEvents()
  12.  
  13. Try
  14. 'CHECKING IF THE INPUT IS A NUMERIC VALUE AND NOT A NULL VALUE.
  15. If IsNumeric(txtzipcode.Text) = True And txtzipcode.Text <> "" Then
  16. 'RETRIEVE THE LOCAL TIME FROM THE WEB SERVICE.
  17. formattime = localtime.LocalTimeByZipCode(txtzipcode.Text)
  18. 'THE LOCAL TIME FROM THE WEB SERVICE IS DELAYED FOR 1 HOUR,
  19. ' THEREFOR, CREATE A FORMULA THAT WILL ADD 1 HOUR IN THE TIME.
  20. addtime = Format(formattime, "hh") + 1
  21. 'SET THE EXACT TIME IN THE TEXTBOX(txtlocaltime).
  22. txtlocaltime.Text = addtime & ":" & Format(formattime, "mm:ss")
  23. Else
  24. MsgBox("Server was unable to process request. The zip code must be correct!", MsgBoxStyle.Exclamation)
  25. End If
  26. Catch exp As Exception
  27. MsgBox("Server was unable to process request. The zip code must be correct!" & exp.Message, MsgBoxStyle.Exclamation)
  28. Exit Sub
  29. Finally
  30.  
  31. 'RESET THE CURSOR TO DEFAULT.
  32. Cursor = Cursors.Default
  33. End Try
  34.  
  35. End Sub
Go to the design views and double click the Form. Do this following code to disable the TextBox that the local time appears.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'DISABLE THE TEXTBOX WHEREIN THE LOCAL TIME WILL APPEAR.
  3. txtlocaltime.Enabled = False
  4. End Sub

Add new comment