Practical C#8 - Displaying Timezones

 

Writing a truly global application would at times require the developer in displaying the different time zones in plain text format.

My previous implementation was to capture the offset in the client and pass this value to a server-side hidden field control which can be read from the server.

<script language="javascript"  type="text/javascript">
    function getOffsetTime() {
        var ourDate = new Date();
        var offsetMinutes = ourDate.getTimezoneOffset().toString();
        var hf_offsetValue = document.getElementById('hf_offsetValue');
        hf_offsetValue.value = offsetMinutes;
    }
</script>

<form id="form1" runat="server">
        <div>
                <asp:Button 
                        ID="Button1" 
                        runat="server" 
                        Text="Click to Display Local Time" 
                        onclick="Button1_Click" /><br />
                <asp:Label ID="label_globalTime" runat="server" />
                <input type="hidden" id="hf_offsetValue" runat="server" />
        </div>
    </form>
</body>

The disadvantage of this is that I have to perform offset value calculation on the server side to obtain the local time zone where the page can possibly be viewed.

protected void Button1_Click(object sender, EventArgs e)
    {     
        DateTime utcNow = DateTime.UtcNow;
        Int32 offsetValue = -(Convert.ToInt32(hf_offsetValue.Value)); 
        label_globalTime.Text =
           utcNow.AddMinutes(offsetValue).ToShortDateString() + " "
            + utcNow.AddMinutes(offsetValue).ToShortTimeString();

    }

Furthermore, this code is difficult to test and validate without someone testing it for you residing in a different time zone.

However, this blog does a great job in explain the differences (theoretical and practical) of the TimeZone and the TimeZoneInfo namespaces in net.
Building up from the concepts learned in the blog, I’ve written a sample page that conveniently displays and enumerates the different time zones relative to your location using the little known namespace TimeZoneInfo.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        displayTimeZones();
    }

    void displayTimeZones()
    {
        DateTimeOffset nowDateTime = DateTimeOffset.Now;
        Response.Write("<strong>Now in Manila: </strong>" + nowDateTime);
        Response.Write("<br /><br /><br />"); 

        ReadOnlyCollection<TimeZoneInfo> timeZones =
                        TimeZoneInfo.GetSystemTimeZones();

        Response.Write("<strong>ALL TIMEZONES: </strong>");
        foreach (TimeZoneInfo indiTimeZoneInfo in timeZones)
        {
            DateTimeOffset newDateTime = 
                 TimeZoneInfo.ConvertTime(nowDateTime,
                     TimeZoneInfo.FindSystemTimeZoneById(indiTimeZoneInfo.Id));
            Response.Write("<br />");
            Response.Write("<strong>Display Name</strong>: " +
                    indiTimeZoneInfo.DisplayName + "<br />");
            Response.Write("<strong>Now in " + indiTimeZoneInfo.StandardName + ":
                    </strong>"
+ newDateTime);
            Response.Write("<br />");
        }
    }
}

With the resulting page as:

Published 08-18-2009 5:11 PM by avcajipe
Filed under: , ,