Get Local Time
Get Local Time by Zip Code in C#
Details: Get Local Time
Many people find it easier to remember local time than standard time. Knowing what time it is in their local area helps them plan their daily activities. To determine local time, you need to know your zip code (or the zip code of the area you live in). After that, you can easily calculate local time from your current location.
In this article we will illustrate how to the C# programming language to create a program that will get the local time by inputting the country’s zip code.
Guide: Get Local Time
1 – We will begin by creating a Windows Form Application in C# using Microscore Studio 2010 following the steps;
– Navigate to File
– Click on New Project
Select Windows Application
2 – Proceed to add 2 textbox;
– txtzipcode: for inputting zip code
– txtlocaltime: for displaying the equivalent local time.
– Now, add a button named Button1.
Your interface should be designed like the image below.
3 – Now proceed to solution explorer, right-click, and add on “Add Service references”.
4 – Again, right-click the folder of “Service references” and tap on the “Add Service references”.
– Input the link “http://www.ripedev.com/webservices/LocalTime.asmx” for the address and type “local_time” in the namespace.
– Tap OK.
5 – Your “Service References” will now be visible.
6 – Proceed to add the below code in the 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 – Also, add the below code in the form_load.
private void Form1_Load(System.Object sender, System.EventArgs e)
{
//disable the textbox wherein the local time will appear.
txtlocaltime.Enabled = false;
}