Click here to Skip to main content
12,457,938 members (50,381 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.8K views
269 downloads
2 bookmarked
Posted

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

, 8 Oct 2012 CPOL
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)

Share

About the Author

Sonali Agarwal
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionThis is awesome Pin
Troy compton13-Jul-16 10:32
memberTroy 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 8 Oct 2012
Article Copyright 2012 by Sonali Agarwal
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid