如何格式化 DateTime 或 TimeZone/TimeZoneInfo 以显示三个字母?

发布于 2024-08-07 12:37:18 字数 428 浏览 4 评论 0原文

我正在使用一个规范,该规范需要一种特殊的日期时间格式,但我还不一定需要使用该格式。

在处理时,必须以如下格式标记 OFX(1 而非 2)日期时间(DTCLIENT 或 DTSERVER):
20071015021529.000

但是,在示例中显示:

20071015021529.000[-8:PST]    

我对第一个使用以下语句的情况没有问题:

DateTime.Now.ToString("yyyyMMddHHmmss.fff")

我什至可以弄清楚如何获取 %z 来获得正确的偏移量。

三字母时区代码是我绊倒的地方。有没有一种简单的方法可以在.net中得到这个,或者我最终会编写代码来输出和解析我需要的内容?

I'm working with a spec that calls for a peculiar Datetime format that I haven't necessarily had to work with yet.

At Process time, an OFX (1 not 2) datetime must be stamped (either DTCLIENT or DTSERVER) in a format like this:
20071015021529.000

however, in the examples it is shown:

20071015021529.000[-8:PST]    

I don't have a problem with the first one using a statement of

DateTime.Now.ToString("yyyyMMddHHmmss.fff")

I can even figure out how to get %z to get the correct offset.

The three letter timezone code is where I'm tripping up. Is there an easy way to get this in .net, or am I going to end up writing code to output and parse what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老娘不死你永远是小三 2024-08-14 12:37:18

三字母时区代码在各个国家之间并不是唯一的,也不是完全标准化的。例如,“CST”可以是“中部标准时间(美国)”或“中国标准时间”的缩写。哪个是正确的取决于您的区域设置。

据我所知,.NET Framework 中没有内置功能来查找此类代码,因此您需要自己实现它。

此外(但您可能知道这一点),您需要小心如何实现此功能。 UTC 偏移量和时区代码之间不一定存在一一对应的关系。例如,UTC-0700 可以是美国山地标准时间 (MST)美国太平洋夏令时 (PDT)。

The three-letter time zone codes are not unique across countries and are not completely standardized. For example, "CST" can be the abbreviation for "Central Standard Time (USA)" or "Chinese Standard Time". Which is correct depends on your locale.

To my knowledge, there is no functionality built into the .NET Framework to look up codes like this, so you will need to implement it yourself.

Additionally (but you probably know this), you need to be careful about how you implement this functionality. There is not necessarily a one-to-one correspondence between UTC offsets and time zone codes. For example, UTC-0700 could be either US Mountain Standard Time (MST) or US Pacific Daylight Time (PDT).

梦屿孤独相伴 2024-08-14 12:37:18

如果您使用 r,它将为您提供 RFC 格式。对你有帮助吗。

String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123

您还可以检查此SO链接,如果它可以帮助你。

If you will use r it will give you RFC format. Will it help you.

String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123

You can also check this SO link, if it can help you.

旧竹 2024-08-14 12:37:18

您需要准确找出它需要哪三个字母代码。例如,Europe/Paris 会提供什么?就个人而言,我会将数据转换为 UTC 并以这种方式格式化,除非您确实需要保留时区信息。

You need to find out exactly which 3 letter codes it needs. What would Europe/Paris give, for example? Personally, I'd convert the data to UTC and format it that way, unless you actually need to preserve the time zone information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文