Click here to Skip to main content
Click here to Skip to main content

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

, 8 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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.

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:Timer runat="server" ID="UpdateTimer" 
   Interval="6000" OnTick="UpdateTimer_Tick" /> 

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

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.  

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)

About the Author

Sonali Agarwal
Web Developer Essential Solve LLC
India India
I am a Web developer, Project Manager. I am:
- Flexible, responsive, creative and enthusiastic.
- Ability to manage multiple initiatives with deadlines
- Willingness to pick up and develop new skills.
- Ability to balance a number of conflicting priorities and make decisions.
- Results oriented - focused on productive and high-yield activities.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Oct 2012
Article Copyright 2012 by Sonali Agarwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid