Get Local Time by Zip Code using C#
Submitted by donbermoy on Sunday, July 20, 2014 - 11:49.
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:
3. After that, go the solution explorer, right click and hit “Add Service references”.
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.
5. Then, you will have now your “Service References”.
6. Now put this code in your Button1_Click.
7. Then put this code in your form_load.
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
- using System.Windows.Forms;
- private void Button1_Click(System.Object sender, System.EventArgs e)
- {
- //declare the variable and create an instance of the web service proxy class
- DateTime formattime = default(DateTime);
- int addtime = default(int);
- //display the proper cursor
- Cursor = Cursors.WaitCursor;
- //processes all the windows message that is currently in the message queue.
- Application.DoEvents();
- try
- {
- //checking if the input is a numeric value and not a null value.
- if (IsNumeric(txtzipcode.Text) == true && txtzipcode.Text != "")
- {
- //retrieve the local time from the web service.
- formattime = System.Convert.ToDateTime(localtime.LocalTimeByZipCode(txtzipcode.Text));
- //the local time from the web service is delayed for 1 hour,
- // therefor, create a formula that will add 1 hour in the time.
- addtime = System.Convert.ToInt32(Format(formattime, "hh") + 1);
- //set the exact time in the textbox(txtlocaltime).
- txtlocaltime.Text = addtime.ToString() + ":" + Format(formattime, "mm:ss");
- }
- else
- {
- MsgBox("Server was unable to process request. The zip code must be correct!", MsgBoxStyle.Exclamation);
- }
- }
- catch (Exception exp)
- {
- MsgBox("Server was unable to process request. The zip code must be correct!" + exp.Message, MsgBoxStyle.Exclamation);
- return;
- }
- finally
- {
- //reset the cursor to default.
- Cursor = Cursors.Default;
- }
- }
- private void Form1_Load(System.Object sender, System.EventArgs e)
- {
- //disable the textbox wherein the local time will appear.
- txtlocaltime.Enabled = false;
- }
Add new comment
- 108 views