Get All Time Zones On Local System In C#

In this article, I will explain how to get all time zones and their details on a local machine using C#.

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).
 
Button-right-click-on-desktop-In-order-to-find-Timezone.jpg

Step 2
 
A Calendar is shown. In this calendar, click on the "Change date and time settings..." option as shown here.

Click-on-change-date-and-time-to-display-timezone.jpg

Step 3
 
A date and time window is opened. In this click on the Change time zone button.
 
click-on-change-time-zone-to-view-all-the-timezone.jpg
 
Step 4
 
Now a following dropdownlist shows all the Time Zones in our current system.
 
List-of-all-the-timeZone.jpg

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: 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"  
  11. BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"  
  12. CellSpacing="2" Height="199px" Width="254px">  
  13. <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />  
  14. <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />  
  15. <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />  
  16. <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />  
  17. <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />  
  18. <SortedAscendingCellStyle BackColor="#FFF1D4" />  
  19. <SortedAscendingHeaderStyle BackColor="#B95C30" />  
  20. <SortedDescendingCellStyle BackColor="#F1E5CE" />  
  21. <SortedDescendingHeaderStyle BackColor="#93451F" />  
  22. </asp:GridView>  
  23. </div>  
  24. </form>  
  25. </body>  
  26. </html>  
Step 3
 
Add the following C# Code on page load. 
  1. public partial class Default2 : System.Web.UI.Page  
  2. {  
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5. GridView1.DataSource = TimeZoneInfo.GetSystemTimeZones();  
  6. GridView1.DataBind();  
  7. }  
  8. }  
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:
 
TimeZone-list-of-current-system-In-GridView.jpg

Step 4
 
To display time zones information in the DropDownList write the aspx. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <asp:DropDownList ID="DropDownList1" runat="server" Height="44px" Width="184px">  
  11. </asp:DropDownList>  
  12. </div>  
  13. </form>  
  14. </body>  
  15. </html>  
Step 5
 
Write the C# code on the page load event handler. 
  1. public partial class Default2 : System.Web.UI.Page  
  2. {  
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5. DropDownList1.DataSource = TimeZoneInfo.GetSystemTimeZones();  
  6. DropDownList1.DataTextField = "DisplayName";  
  7. DropDownList1.DataValueField = "Id";  
  8. DropDownList1.DataBind();  
  9. }  
  10. }  
Output
 
The output of the preceding code looks like this:
 
TimeZone-List-of-current-system-In-Dropdownlist.jpg

Summary
 
In this article, I created a simple ASP.NET Web application that loads all time zones and their information using C#.