ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Get Local Time Using Zip Code in VB.NET

Today, I will teach you how to get the local time by using a zip code in VB.NET. With this you will be able to identify what are the local time of all the countries using their zip codes. In here, I used the Web Service to get the local time in a certain country.

 

Let’s begin:

 

Open the Visual Basic, create a new Windows Form Application and do the Form just like this.
zipcodeform1
After setting up the Form, go the solution explorer, right click and hit “Add Service references”.
zipcodeform2

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 server’s address and hit ok.
zipcodeform3

You will now have a “Service References”.
zipcodeform4

 

Double click the “Go” Button on the Form. You have to set up the server to get the local time to their following Zip Codes in the click event handler of the Button.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. 'SET THE VARIABLE TO CREATE AN INSTANCE OF THE WEB SERVICE PROXY CLASS
  4. Dim local_time As New ServiceReference1.LocalTimeSoapClient
  5. Dim format_time As Date
  6. Dim add_time As Integer
  7.  
  8. 'SET THE PROPER CURSOR
  9. Cursor = Cursors.WaitCursor
  10. 'SET THE PROCESSES OF ALL THE WINDOWS MESSAGE THAT IS CURRENTLY IN THE MESSAGE QUEUE.
  11. Application.DoEvents()
  12.  
  13. Try
  14. 'FILTERING THE NUMERIC VALUE.
  15. If IsNumeric(txtzipcode.Text) = True And txtzipcode.Text = "" Then
  16. 'SET THE ServiceReference TO RETRIEVE THE LOCAL TIME FROM THE WEB SERVICE.
  17. format_time = local_time.LocalTimeByZipCode(txtzipcode.Text)
  18. 'SET A FORMULA THAT WILL BE ADDED 1 HOUR IN THE TIME
  19. 'BECAUSE THE LOCAL TIME IN THE WEB SERVICE IS DELATED FOR 1 HOUR.
  20. add_time = Format(format_time, "hh") + 1
  21. 'DISPLAY THE EXACT TIME IN THE TEXTBOX(txtlocaltime).
  22. txttime.Text = add_time & ":" & Format(format_time, "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. You have 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. 'DISABLING THE TEXTBOX.
  3. txtlocaltime.Enabled = False
  4. End Sub

 

Download the complete source code here.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!