Introduction
A Time Zone is a region on Earth that has a uniform Standard time for legal, commercial and social purposes. The TimeZoneInfo class represents any time zone in the world. This class has various methods and properties that we use to find Time Zone 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) as in:
Step 2
A callendar is shown. In this calendar click on the "Change date and time settings..." option as shown in the following:
Step 3
A date and time window is opened. In this click on the Change time zone button, as in:
Step 4
Now a following dropdownlist shows all the TimeZones in our current system.
Our work is to display all this information in the ASP .Net application using C#.
So let's use the following steps to display all the TimeZones in a C# ASP.Net application. In this I simply explain how to display all the TimeZone information in our application, display all the information in the Dropdownlist or the GridView.
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 the TimeZones in the GridView, add a GridView control in your ASP.Net application as:
<%@ 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 C# Code for this as:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = TimeZoneInfo.GetSystemTimeZones();
GridView1.DataBind();
}
}
In this the class TimeZoneInfo is used to perform functions on Time Zone and the GetSystemTimeZone method is used to get all the time zone values of the local system.
Output
The output of the preceding code is as:
Step 4
To display all the TimeZone information in the DropDownList write the aspx code as:
<%@ 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 for this as:
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:
Summary
In this article I simply give the introduction of TimeZone and explained how we get all the TimeZone information in our local system.