我知道 System.TimeZone 类以及 DateTime.ToString() 方法.我找不到的是一种将 DateTime 转换为字符串的方法,该字符串除了时间和日期信息之外,还包含三个字母的时区缩写(实际上,与 StackOverflow 的相对工具提示大致相同)时间显示有效).
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).
为了让每个人都能轻松学习和使用示例,让我们继续 StackOverflow 示例.如果您查看显示相对时间的工具提示,它会显示完整日期、时间(包括 12 小时格式的秒数)、AM/PM 名称,然后是三个字母的时区缩写(在他们的情况下,协调世界时).我意识到我可以通过使用内置方法轻松获取 GMT 或 UTC,但我真正想要的是本地时间 —在这种情况下,在 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".我意识到我可以轻松地对其进行硬编码,但出于稳健性的考虑,我真的更喜欢基于运行代码的操作系统环境设置的服务器的解决方案.
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")
其中显示:
2008 年 10 月 7 日下午 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 03:40:31 PM CDT
10/07/2008 03:40:31 PM CDT
我可以使用 System.TimeZone.CurrentTimeZone 并使用它来正确显示Central Daylight Time"但是......为了简洁起见,这有点太长了.然后我是否坚持编写字符串操作例程来去除空格和任何非大写字母?虽然这可能行得通,但对我来说这似乎令人难以置信......
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!