Introduction
A Time Zone is a region on Earth that has a uniform standard time for legal, commercial and social purposes. There are more than 130 time zones. C# TimeZoneInfo class represents any time zone in the world. This class has various methods and properties that we use to find time zones and their values, times and much more. The TimeZone information in our current system can be viewed by the following.
Step 1. Go to the bottom-right corner of your system and click on the time (or date and time).
Step 2. A Calendar is shown. In this calendar, click on the "Change date and time settings..." option as shown here.
Step 3. A date and time window is opened. In this click on the Change time zone button.
Step 4. Now a following dropdownlist shows all the Time Zones in our current system.
Time Zone Application
My goal is to display all this information in an ASP.NET Web application using C#.
So let's use the following steps to create an appliation that will display all time zones and their info in GridView and DropDownList controls.
Step 1. Open Visual Studio and click on File->New->Website. A window is shown; in it select the ASP.NET application under Visual C# and give your application a name and then click ok.
Step 2. To display all TimeZones in the GridView, add a GridView control in your ASP.Net application as following.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" Height="199px" Width="254px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 3. Add the following C# Code on page load.
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = TimeZoneInfo.GetSystemTimeZones();
GridView1.DataBind();
}
}
In the above code, TimeZoneInfo is used to return all timezones on current system. The GetSystemTimeZone method returns a collection that is bound direct to a GridView control using its DataSource property.
Output
The output of the preceding code generates the following
Step 4. To display time zones information in the DropDownList write the aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Height="44px" Width="184px">
</asp:DropDownList>
</div>
</form>
</body>
</html>
Step 5. Write the C# code on the page load event handler.
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = TimeZoneInfo.GetSystemTimeZones();
DropDownList1.DataTextField = "DisplayName";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
}
Output
The output of the preceding code looks like this
Summary
In this article, I created a simple ASP.NET Web application that loads all time zones and their information using C#.