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

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

问题描述

我知道 System.TimeZone 类以及 DateTime.ToString()方法。我不能找到的是一种将DateTime转换为字符串的方法,除了时间和日期信息之外,还包含三个字母的时区缩写(事实上,StackOverflow的工具提示相对于相对时间显示工作)。



为了让每个人都可以轻松地跟踪和消费,我们继续使用StackOverflow示例。如果您查看相对时间显示的工具提示,则会显示完整的日期,包括十二小时格式的秒数,AM / PM的名称,然后是三个字母的时区缩写(在这种情况下,Coordinated世界时间)。我意识到我可以通过使用内置的方法轻松得到GMT或UTC,但是我真正想要的是时间,因为它在本地—在这种情况下,在网络服务器上。



如果我们的网络服务器正在运行Windows Server 2k3并将其时区设置为CST(或直到夏令时切换回来,CDT是吗?),我希望我们的ASP.NET网络应用程序显示DateTimes相对于该时区,并格式化以在最后显示CST。我意识到我可以很容易地硬编码这个,但是为了稳健性,我更喜欢基于运行代码操作系统环境设置的服务器的解决方案。



现在,除了使用以下代码的时区缩写之外,我还有其他一切:

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

哪个显示:



10/07/2008 03:40:31 PM



所有我想要的(这不是很多,承诺!)是为了说: / p>

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



我可以使用System.TimeZone.CurrentTimeZone和使用它来正确显示中央夏令时,但是为了简洁起见,这太长了。然后我卡住了一个字符串操作程序去除白色空间和任何非大写字母?虽然这可能会起作用,但这似乎让我难以置信。



Google搜索,并在这里环顾四周,没有任何适用于我的具体问题。

解决方案

这是我刚刚解决的快速黑客方法。

  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(sSplit中的字符串)
if(s.Length> = 1)
sNewName + = s.Substring(0,1);

return sNewName;
}


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.

解决方案

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;
}

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

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