如何使用缩写时区显示日期时间? [英] How to display DateTime with an abbreviated Time Zone?

查看:225
本文介绍了如何使用缩写时区显示日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 System.TimeZone 类作为还有的DateTime.ToString()方法。我一直没能找到一种方法,日期时间转换为字符串,除了时间和日期信息,包含三个字母的时区缩写(事实上,几乎相同的方式计算器的工具提示相对时间显示作品)。

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).

要做出表率容易让大家遵循,以及消费,让我们继续用计算器的例子。如果你看一下,关于相对时间显示工具提示,它与完整的日期显示,包括12小时的格式,一个AM / PM指示,然后将三个字母的时区缩写秒(在他们的情况的时候,协调世界时)。我知道我可以很容易地得到GMT或UTC通过使用内置的方法,但我真正希望的是时间,因为它是在本地和mdash;在这种情况下,在Web服务器上。

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.

如果我们的网络服务器运行Windows Server 2K3,拥有它的时区设置为CST(或者,直到节省日光切换回,CDT是什么呢?),我想我们的ASP.NET Web应用程序,以显示相对于该时区以及格式化就结束显示CST创建DateTime对象。我知道我可以很容易地硬code这一点,但在稳健的利益,我真的preFER解决方案基于运行code的操作系统环境设置在服务器上。

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.

现在,我拥有​​了一切,但使用下面的code时区缩写:

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下午3点40分31秒

10/07/2008 03:40:31 PM

我要的(!和它的并不多,承诺),是因为它说:

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

10/07/2008下午3点40分31秒CDT

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

我可以使用System.TimeZone.CurrentTimeZone,并用它来正确显示中部夏令时间,但...这是一个有点太长了简洁的缘故。难道我那么卡住写一个字符串处理例程来去掉空格以及任何非大写字母?虽然这可能的工作,这似乎令人难以置信的劈到我...

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...

<一个href="http://www.google.com/search?hl=en&safe=off&q=C%23+time+zone+abbreviation&btnG=Search">Googling环视就到这里并没有产生任何适用于我的具体问题。

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

推荐答案

这是我刚才提出来解决这个我快速的破解方法。

Here's my quick hack method I just made to work around this.

public static String TimeZoneName(DateTime dt)
{
    String sName = TimeZone.CurrentTimeZone.IsDaylightSavingTime(dt) 
        ? TimeZone.CurrentTimeZone.DaylightName 
        : TimeZone.CurrentTimeZone.StandardName;

    String sNewName = "";
    String[] sSplit = sName.Split(new char[]{' '});
    foreach (String s in sSplit)
        if (s.Length >= 1)
            sNewName += s.Substring(0, 1);

    return sNewName;
}

这篇关于如何使用缩写时区显示日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆