How to display DateTime with an abbreviated Time Zone?

前端 未结 8 911
长情又很酷
长情又很酷 2020-12-08 19:02

I am aware of the System.TimeZone class as well as the many uses of the DateTime.ToString() method. What I haven\'t been able to find is a way to convert a DateTime to a st

8条回答
  •  醉梦人生
    2020-12-08 19:29

    It depends on the level of robustness that you need.

    You'll probably need some kind of hack either way. An easy way would be to split the string by spaces and then concatenate the first letter of each word. i.e.

    string[] words = tzname.Split(" ".ToCharArray());
    string tzabbr = "";
    foreach (string word in words)
       tzabbr += word[0];
    

    That won't work for every time zone on the planet, but it will work for most of them. If you need it more robust then you'll probably need to create a map that maps time zone names to their abbreviations.

提交回复
热议问题