How to display DateTime with an abbreviated Time Z

2019-01-17 14:19发布

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 string that, in addition to the time and date info, contains the three-letter Time Zone abbreviation (in fact, much the same way StackOverflow's tooltips for relative time display works).

To make an example easy for everyone to follow as well as consume, let's continue with the StackOverflow example. If you look at the tooltip that displays on relative times, it displays with the full date, the time including seconds in twelve-hour format, an AM/PM designation, and then the three-letter Time Zone abbreviation (in their case, Coordinated Universal Time). I realize I could easily get GMT or UTC by using the built-in methods, but what I really want is the time as it is locally — in this case, on a web server.

If our web server is running Windows Server 2k3 and has it's time zone set to CST (or, until daylight saving switches back, CDT is it?), I'd like our ASP.NET web app to display DateTimes relative to that time zone as well as formatted to display a "CST" on the end. I realize I could easily hard-code this, but in the interest of robustness, I'd really prefer a solution based on the server running the code's OS environment settings.

Right now, I have everything but the time zone abbreviation using the following code:

myDateTime.ToString("MM/dd/yyyy hh:mm:ss tt")

Which displays:

10/07/2008 03:40:31 PM

All I want (and it's not much, promise!) is for it to say:

10/07/2008 03:40:31 PM CDT

I can use System.TimeZone.CurrentTimeZone and use it to correctly display "Central Daylight Time" but... that's a bit too long for brevity's sake. Am I then stuck writing a string manipulation routine to strip out white-space and any non-uppercase letters? While that might work, that seems incredibly hack to me...

Googling and looking around on here did not produce anything applicable to my specific question.

8条回答
对你真心纯属浪费
2楼-- · 2019-01-17 15:02

There is a freely available library, TZ4NET, which has these abbreviations available. Prior to .NET 3.5, this was one of the only alternatives for converting between timezones as well.

If you don't want a seperate library, you could certainly generate a map of reasonable abbreviations using the TimeZoneInfo classes, and then just supply those to your user.

查看更多
3楼-- · 2019-01-17 15:02

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]);
}
查看更多
登录 后发表回答