Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using .net framework 2.0 and i want to display time zone abbreviation eg. Arabian Standard Time as AST but my code below displays the full timezone name
Please help

<div style="float:right"><asp:Label ID="Label1" runat="server" ><%=DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString()+TimeZone.CurrentTimeZone.StandardName%></asp:Label></div>
Posted
Updated 28-Apr-11 15:59pm
v2

First, using class System.TimeZoneInfo instead of System.TimeZone as it is recommended in the MSDN help page of the last one.

You current time zone is returned by System.TimeZoneInfo.Local; the names of the same time zone are returned by the properties DaylightName, DisplayName, Id and StandardName.

See http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx[^].

Which of the names represent the abbreviated form! No one! Same thing as with System.TimeZone. Why?

I think it is understandable. Look at the list of all abbreviated names of the time zones:
http://www.timeanddate.com/library/abbreviations/timezones/[^].

Your can see that there is no one-to-one correspondence between abbreviations and time zones. More exactly, more than one time zone can be mapped onto the same abbreviated form; for example, "AMT" means UTC+4 (Armenia Time) and UTC-4 (Amazon Time) at the same time. Same thing with "EDT" and other abbreviations. On other words, abbreviated forms are useless for identification purposes, so their use should be best avoided as they can cause confusion. Even if you use abbreviated form of the name, it should be accompanied by UTC notation to avoid ambiguity.

It is known that .NET does not support abbreviated forms; I think — for a good reason.

There is an old W3C RFC822 standard http://www.w3.org/Protocols/rfc822/#z28[^] (1977) which described several abbreviations which cover only US; and this subset does not create ambiguity.

There are different discussion on various hacks which attempt to create the abbreviated name by automatic abbreviation of the standard names. I don't want to reference such codes; you can easily find them. Nothing can guarantee they match knows abbreviated names. One can use the table referenced above, list all time zones using System.TimeZoneInfo.GetSystemTimeZones, manually type abbreviated names, create a look-up table in the form of System.Collections.Generic.Dictionary<System.TimeZoneInfo, string>; (not the other way around, due to ambiguity described above), populate it once and persist on the disk. During run-time, you can populate the dictionary from the file and use it to find abbreviated name by the "real" time zone. I would not recommend to waste time on it.

—SA
 
Share this answer
 
v2
Comments
Tarakeshwar Reddy 28-Apr-11 22:43pm    
Good and detailed information. My 5
Sergey Alexandrovich Kryukov 29-Apr-11 0:01am    
Thank you, Tarakeshwar.
--SA
Sandeep Mewara 29-Apr-11 0:14am    
5++
:)
Sergey Alexandrovich Kryukov 29-Apr-11 0:15am    
Thank you, Sandeep.
--SA
Unfortunately, there is no easy built-in way of doing this that I know of. However, you could put something together yourself. Here's an example:


C#
public static class zoneShort
    {
        public static string Convertion(this TimeZoneInfo Source)
        {
            var Map = new Dictionary<string, string>()
            {
            {"india standard time","ist"},
            {"eastern standard time","est"},
            {"mountain standard time","mst"},
            {"central standard time","cst"},
            {"pacific standard time","pst"}
            //etc...
            };
            return Map[Source.Id.ToLower()].ToUpper();
        }
    }


use as follows :

C#
string zoneAbbreviation = TimeZoneInfo.Local.Convertion();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Apr-11 21:38pm    
Not very useful. The abbreviated names are ambiguous.
Please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900