Get Local Time by Zip Code using C#

Today in C#, I will teach you how to create a program that will get the local time if we input the country's zip code using C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Add 2 textbox named txtzipcode by inputting zip code and txtlocaltime to display the equivalent local time. Then insert one button named Button1. Design your interface like this: design 3. After that, go the solution explorer, right click and hit “Add Service references”. output 4. 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. Add reference 5. Then, you will have now your “Service References”. Add reference 6. Now put this code in your Button1_Click.
  1. using System.Windows.Forms;
  2.  
  3.  
  4.  
  5. private void Button1_Click(System.Object sender, System.EventArgs e)
  6. {
  7.        
  8.         //declare the variable and create an instance of the web service proxy class
  9.         local_time.LocalTimeSoapClient localtime = new local_time.LocalTimeSoapClient();
  10.         DateTime formattime = default(DateTime);
  11.         int addtime = default(int);
  12.        
  13.         //display the proper cursor
  14.         Cursor = Cursors.WaitCursor;
  15.         //processes all the windows message that is currently in the message queue.
  16.         Application.DoEvents();
  17.        
  18.         try
  19.         {
  20.                 //checking if the input is a numeric value and not a null value.
  21.                 if (IsNumeric(txtzipcode.Text) == true && txtzipcode.Text != "")
  22.                 {
  23.                         //retrieve the local time from the web service.
  24.                         formattime = System.Convert.ToDateTime(localtime.LocalTimeByZipCode(txtzipcode.Text));
  25.                         //the local time from the web service is delayed for 1 hour,
  26.                         // therefor, create a formula that will add 1 hour in the time.
  27.                         addtime = System.Convert.ToInt32(Format(formattime, "hh") + 1);
  28.                         //set the exact time in the textbox(txtlocaltime).
  29.                         txtlocaltime.Text = addtime.ToString() + ":" + Format(formattime, "mm:ss");
  30.                 }
  31.                 else
  32.                 {
  33.                         MsgBox("Server was unable to process request. The zip code must be correct!", MsgBoxStyle.Exclamation);
  34.                 }
  35.         }
  36.         catch (Exception exp)
  37.         {
  38.                 MsgBox("Server was unable to process request. The zip code must be correct!" + exp.Message, MsgBoxStyle.Exclamation);
  39.                 return;
  40.         }
  41.         finally
  42.         {
  43.                
  44.                 //reset the cursor to default.
  45.                 Cursor = Cursors.Default;
  46.         }
  47.        
  48. }
7. Then put this code in your form_load.
  1. private void Form1_Load(System.Object sender, System.EventArgs e)
  2. {
  3.         //disable the textbox wherein the local time will appear.
  4.         txtlocaltime.Enabled = false;
  5. }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment