Uncategorized

Time Localization using .Net TimeZone API in C#

A time zone is a geographical region in which the same time is used.

You can use the TimeZone class to retrieve information about the current time zone, and to convert times from local time to Coordinated Universal Time (UTC) or vice versa. However, you cannot use the TimeZone class to represent time zones other than the local zone or to handle date and time conversions from one time zone to another. For this purpose, use the TimeZoneInfo class. You can use this class to retrieve information on any time zone defined on the local system, to create custom time zones, and to convert times from one time zone to another.
The TimeZone class supports only a single daylight saving time adjustment rule for the local time zone. As a result, the TimeZone class can accurately report daylight saving time information or convert between UTC and local time only for the period in which the latest adjustment rule is in effect. In contrast, the TimeZoneInfo class supports multiple adjustment rules, which makes it possible to work with historic time zone data.

Lets go to an example .

If you have an Application which will be used in so many Countries and every User comments on a Note in your Site and When Another User logs in , He wants to see the comment Time in His own TimeZone , not in your time Zone . Like If a person from Switzerland comments on a Note at 9.00 AM , after that when I log in I want to see that in my Time Zone Of Bangladesh which should be 1.00 PM . I used same concept few years back in one of My Sites and its awesome ūüôā

Its better to Keep the DateTime Columns as UTC Date and Then convert to User’s own TimeZone By his TimeZoneId using .Net TimeZone API .

Your Site Can have a UI which would be used by The User to Set his Own Time Zone when he gets Registered Into your Site. So That UI can be like below :::

ddlTimeZone

And You can easily populate The Time Zone List DropDown .

Here is Asp.Net markup for that :

<asp:DropDownList ID="ddlTimeZoneId" runat="server">
                </asp:DropDownList>

And Binding DropDown Code will be like :::

private void LoadTimeZoneDropDown()
        {
            ddlTimeZoneId.DataSource = clsTimeZone.GetTimeZones();
            ddlTimeZoneId.DataTextField = "TimeZoneName";
            ddlTimeZoneId.DataValueField = "TimeZoneID";
            ddlTimeZoneId.DataBind();
        }

This clsTimeZone is my custom Class which is using .Net APIs . And I used This below Method to Get the Time Zone List .

public static Collection<structTimeZone> GetTimeZones()
        {
            Collection<structTimeZone> structTimeZoneColl = new Collection<structTimeZone>();

            ReadOnlyCollection<TimeZoneInfo> timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
            foreach (TimeZoneInfo timeZoneInfo in timeZoneInfos)
            {
                structTimeZoneColl.Add(new structTimeZone { TimeZoneName = timeZoneInfo.DisplayName, TimeZoneID = timeZoneInfo.Id });
            }
            return structTimeZoneColl;
        }

public struct structTimeZone
    {
        public string TimeZoneName { get; set; }
        public string TimeZoneID { get; set; }
    }

From here , User can set his Own Time Zone from the List which .Net Framework or Windows or Microsoft supports.

So your User Table should have Column like this :::

TimeZoneIdColumn

So your Initial Setup is done .

Now When Any User from Switzerland saves Note to Notes Table , then don’t Use Local DateTime rather use UTC Time .

It can be done by using this SQL In your StoredProcedure .

GETUTCDATE()

So your DB Table will hold UTC , so you can easily convert that to any Localized format , to any Time Zone by your TimeZoneId of that User .

So When need to show the Time to UI , just call This method which will return Local time Zone Info . In my Case I did something like this :::

DateTime localTime = clsTimeZone.ConvertToLocalTimeFromUtcTime(utcDate, timeZoneId);

And the implementation of the Method is just simple like below :::

public static DateTime ConvertToLocalTimeFromUtcTime(DateTime utcDate, string timeZoneId)   
        {
            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, cstZone);
            return cstTime;
        }

Just Show that Local Time To The UI .
So you are done ūüôā

Cheers

Standard

Leave a Reply

Your email address will not be published. Required fields are marked *