C# - How to get Time Zone in asp .net application?

random

C# - How to get Time Zone in asp .net application?

TimeZone.CurrentTimeZone Property to Select TimeZone in Dropdown


A time zone is a geographical region in which the same time is used. The TimeZone class recognizes only the local time zone, and can convert times between Coordinated Universal Time (UTC) and local time.



TimeZoneInfo class supports following operations: -
  • Retrieving a time zone that is already defined by the operating system.
  • Enumerating the time zones that are available on a system.
  • Converting times between different time zones.
  • Creating a new time zone that is not already defined by the operating system.
  • Serializing a time zone for later retrieval.

For example find the source code below :

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>C# - How to get time zone in Asp .Net application? </title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: 'Trebuchet MS'">
       -> Local date & time :
        <asp:Label ID="lblLocalTime" runat="server" BackColor="#DEF5FF" ForeColor="black"></asp:Label>
        <br />
        <br />
        <asp:DropDownList ID="ddlTimeZone" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlTimeZone_SelectedIndexChanged"
            AppendDataBoundItems="true">
            <asp:ListItem Text="Select a TimeZone" Value="Default value" />
        </asp:DropDownList>
        <br />
        <br />
      ->  Selected zone date & Time :
        <asp:Label ID="lblTimeZone" runat="server" BackColor="#DEF5FF" ForeColor="black"></asp:Label>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
using System.Collections.ObjectModel;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblLocalTime.Text = DateTime.Now.ToString();

            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();
        }
    }

Download the source code click here ..
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading .. :)

C# - How to get Time Zone in asp .net application? Reviewed by Ravi Kumar on 2:33 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.