How do I format DateTime or TimeZone/TimeZoneInfo to display Three letters?

泄露秘密 提交于 2019-12-01 09:38:35

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/1527706/how-do-i-format-datetime-or-timezone-timezoneinfo-to-display-three-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!