Question

I'm trying to get the long name for the timezone from System.TimeZone. This is how I'm trying to do it:

Console.WriteLine(System.TimeZone.CurrentTimeZone.DaylightName);

And the result that i'm getting in console is

EST

But what I need is the long name:

Eastern Standard Time

I looked into C# documentation (http://msdn.microsoft.com/en-us/library/system.timezone.currenttimezone(v=vs.110).aspx) and noticed that the .Net library is returning what I need but for some reason Xamarin (mono) just gives me the abbreviation.

Is there a way around that?

Was it helpful?

Solution

At the end of the day, I went through a lot of pain to figure this out and ended up with a converter on the Web and NOT on the android or device:

    private TimeZoneInfo ConvertTimeZone(string timeZoneString)
    {
        ReadOnlyCollection<TimeZoneInfo> timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
        if (Regex.IsMatch(timeZoneString, "[A-Z]{3,5}[0-9]{0,2}") && !Regex.IsMatch(timeZoneString, "[A-Z]{3,5}[-]{1}[0-9]{0,2}"))
        {
            List<string> abbreviations = new List<string>();

            // create the list of abbreviations for TimeZoneInfos
            timeZoneInfos.ToList().ForEach(
                x =>
            {
                if (!Regex.IsMatch(x.StandardName, "[A-Z0-9]{3,4,5}"))
                {
                    string fullString = x.StandardName.Replace(" ", string.Empty);
                    string[] split = Regex.Split(fullString, "[a-z]|[()-.]");
                    abbreviations.Add(string.Concat(split));
                }
            });

            return timeZoneInfos[abbreviations.IndexOf(timeZoneString)];
        }
        else
        {
            return timeZoneInfos.Where(x => x.StandardName == timeZoneString).First();
        }
    }

The purpose of this was to get to show my data on the Map with the correct time zone and this did it.

Warning: This will probably work on most of the North American time zones but I cannot guarantee that it will work all across the world. Windows does have 102 time-zones and I just tested a few. I basically created a map for abbreviations.

OTHER TIPS

I know it's a bit older, but for those who needs to get the timezone name use

Java.Util.TimeZone.Default.ID

This will give the timezone name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top