How to display DateTime with an abbreviated Time Zone?

前端 未结 8 957
长情又很酷
长情又很酷 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:40

    Ok, It's been 4 years (and almost a week), it's time we brought LINQ into the discussion...

    Putting together Criag's and Bob's ideas...

    public static String TimeZoneName2(DateTime dt)
    {
        var return ToCurrentTimeZoneShortString(dt)
                     .Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
        return sSplit.Aggregate("", (st,w)=> st +=w[0]);
    }
    

    Unless you can trust TimeZone to never return a string with two consecutive spaces:

    public static String TimeZoneName3(DateTime dt)
    {
        return ToCurrentTimeZoneShortString(dt).Split(' ')
                     .Aggregate("", (st,w)=> st +=w[0]);
    }
    

提交回复
热议问题