Get Local Time by Zip Code in C#

May 31, 2020
Tutorials
zip code local time 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#.

 Let’s start this tutorial!

1. Create a Windows Form Application in C# for this tutorial by 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. Afterwards, 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.

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
    local_time.LocalTimeSoapClient localtime = new local_time.LocalTimeSoapClient();
    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;
    }
 
}

7. Then put this code in your form_load.

private void Form1_Load(System.Object sender, System.EventArgs e)
{
    //disable the textbox wherein the local time will appear.
    txtlocaltime.Enabled = false;
}

Output:

output

Facebook Comments

Leave a Reply

Your email address will not be published.