Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Tip/Trick

Display local time of various cities in world using Timer control and TimeZone class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Oct 2012CPOL1 min read 14.9K   346   2   1
Here I am showing the local time in various timezones in the world

Introduction    

A few weeks back I had a requirement in ASP.NET in which I had to get the time of all the cities around the world. There is an hourly scheduler that fires SMS and want it to support world times and country codes.

There're two components needed here:

  1. A drop down of countries with timezones.
  2. SMS's for specific timezone are to be sent at 9am.  The server time is currently +10 AEST (this may change) and the scheduler fires every hour (i.e., 9am, 10am, 11am etc). I want a simple formula that calculates which timezones are at 9am (calculated by using the current server's time).

Background    

To get my work done I used 

  1. TimeZoneInfo class to get the local time in all over the world. (http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx
  2. Timer class which is executed at regular intervals infinitely in an application.

Using the Code  

I used TimeZoneInfo.ConvertTimeBySystemTimeZoneId in the sample code. It converts a time from one time zone to another based on time zone identifiers.

C#
private void ShowTime()
{
    DataTable dt = new DataTable();
    int i=0;
    string time = string.Empty;
    string hour = string.Empty;
    string minutes = string.Empty;
    dt.Columns.Add("ID"); dt.Columns.Add("TimeZone");
    lblLocalTime.Text = DateTime.Now.ToString();
    ReadOnlyCollection<TimeZoneInfo> tzi;
    tzi = TimeZoneInfo.GetSystemTimeZones();
    lblTimeZone.Text = "<table border='1'>";
    foreach (TimeZoneInfo timeZone in tzi)
    {
        dt.Rows.Add(i, timeZone.DisplayName);
        i++;
        time = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, TimeZoneInfo.Local.Id, 
           timeZone.Id).ToString().Substring(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, 
           TimeZoneInfo.Local.Id, timeZone.Id).ToString().IndexOf(" ") + 1); ;
        hour = time.Substring(0, time.IndexOf(":"));
        minutes = time.Substring(time.IndexOf(":") + 1, time.Length - time.IndexOf(" ") - 1);

        if (hour.Equals("9"))// && minutes.Equals("00"))
        {
            lblTimeZone.Text += "<tr><td>";
            lblTimeZone.Text += timeZone.DisplayName;
            lblTimeZone.Text += "</td><td>";
            lblTimeZone.Text += TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
                  DateTime.Now, TimeZoneInfo.Local.Id, timeZone.Id).ToString();
            lblTimeZone.Text += "</td><td>";
            lblTimeZone.Text += time;
            lblTimeZone.Text += "</td><td>";
            lblTimeZone.Text += timeZone.DisplayName.Substring(timeZone.DisplayName.IndexOf(")")+1);
            lblTimeZone.Text += "</td><td>";
            lblTimeZone.Text += hour + ", " + minutes;
            lblTimeZone.Text += "</td></tr>";
        }
    }
    lblTimeZone.Text += "</table>";
    ddlCountry.DataSource = dt;
    ddlCountry.DataTextField = "TimeZone";
    ddlCountry.DataValueField = "ID";
    ddlCountry.DataBind();
}

Once the Timer control is added in the ASPX page, the page will postback at regular intervals you specify in the 'Interval' property of the control:

ASP.NET
<asp:Timer runat="server" ID="UpdateTimer" 
   Interval="6000" OnTick="UpdateTimer_Tick" /> 

Here the UpdateTimer_Tick method will be called after every 6 seconds. 

C#
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
    ShowTime();
}

If the time in any city is 9am then that city name will be displayed in a grid.  

Image 1

Points of Interest 

This article provides a very basic introduction to the TimeZoneInfo and Timer classes.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Essential Solve LLC
India India
I am flexible, responsive, creative and enthusiastic with ability to manage multiple initiatives with deadline. I have willingness to pick up and develop new skills and ability to balance a number of conflicting priorities and make decisions. I am results oriented - focused on productive and high-yield activities.

Comments and Discussions

 
QuestionThis is awesome Pin
Troy compton13-Jul-16 10:32
Troy compton13-Jul-16 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.