<%@ Page Title=".Net Time Zones" Language="C#" AutoEventWireup="true"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = getTZs();
GridView1.DataBind();
if (!Page.IsPostBack)
{
lblLocalTime.Text = DateTime.Now.ToString();
System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> tzi;
tzi = TimeZoneInfo.GetSystemTimeZones();
foreach (TimeZoneInfo timeZone in tzi)
{
ddlTimeZone.Items.Add(new ListItem(timeZone.DisplayName, timeZone.Id));
}
}
}
protected void ddlTimeZone_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlTimeZone.SelectedIndex > 0)
{
DateTime dt = DateTime.Now;
lblTimeZone.Text = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
TimeZoneInfo.Local.Id, ddlTimeZone.SelectedValue).ToString();
}
}
protected static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> getTZs()
{
System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> tzi;
tzi = TimeZoneInfo.GetSystemTimeZones();
return tzi;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title>Time Zones</title>
<style type="text/css">
body
{
background: #FEFEFF;
font-size: .80em;
font-family: "Trebuchet MS Gothic", Arial, Helvetica, Verdana, sans-serif;
margin: 0px;
padding: 0px;
color: Black;
}
</style>
</head>
<body>
<div>
<h2>.Net TimeZones</h2>
Local Time: <asp:Label ID="lblLocalTime" runat="server" Text=""></asp:Label>
Converted Time: <asp:Label ID="lblTimeZone" runat="server" Text=""></asp:Label>
<br />
<form id="Form1" runat="server">
<div style="float:left;">
<asp:ListBox ID="ddlTimeZone" runat="server" AutoPostBack="True" Rows="4"
onselectedindexchanged="ddlTimeZone_SelectedIndexChanged"
AppendDataBoundItems="true" >
<asp:ListItem Text="Select a TimeZone by DisplayName" Value="Default value" />
</asp:ListBox>
</div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
EnableTheming="False" EnableViewState="False" ForeColor="#333333"
GridLines="None" AlternatingRowStyle-BackColor="PaleGoldenrod" />
</form>
</div>
</body>
</html>