Timezone Abbreviations

前端 未结 7 1523
情话喂你
情话喂你 2020-11-28 08:07

TimeZoneInfo does not provide abbreviation or a short name for a given Timezone. The only good way to do it is to have a dictionary that will map abbreviations

7条回答
  •  清酒与你
    2020-11-28 08:30

    This is is helpful for anyone using Xamarin for iOS or Android because according to the documentation "The display name is localized based on the culture installed with the Windows operating system." When using this in the Central Time Zone, for the DateTime "2016-09-01 12:00:00 GMT", this function returns "CDT" which is exactly what I needed when I came upon this question.

    public static string GetTimeZoneAbbreviation(DateTime time)
    {
        string timeZoneAbbr;
        if(time.IsDaylightSavingTime() == true)
        {
            timeZoneAbbr = TimeZoneInfo.Local.DaylightName;
        }
        else
        {
            timeZoneAbbr = TimeZoneInfo.Local.StandardName;
        }
    
        return timeZoneAbbr;
    }
    

提交回复
热议问题