How to format a DateTime like “Oct. 10, 2008 10:43am CST” in C#

不羁的心 提交于 2019-12-22 09:07:01

问题


Is there a clean way to format a DateTime value as "Oct. 10, 2008 10:43am CST".

I need it with the proper abbreviations and the "am" (or "pm") in lower case etc etc.

I've done it myself but it's ugly so I'm looking for a different take on it.

Thanks.


回答1:


Since the "tt" format string specifier only outputs upper case, you'll have to modify that yourself. Also, DateTimes do not store the name of the timezone, only an offset.

DateTime dt = DateTime.Now;
string ampm = dt.ToString("tt").ToLower();
string output = string.Format("{0:MMM. d, yyyy h:mm}{1}", dt, ampm);



回答2:


DateTimeObject.ToString("MMM. dd, yyyy hh:mmtt");

not sure about CST.

If you want more combinations see this link:

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm




回答3:


Assuming your server is configured to CST:

string format = dateTime.ToString("mmm. dd, YYYY HH:MM tt ")
    .Replace(" AM ", "am")
    .Replace(" PM ", "pm") +
    " CST";



回答4:


Will this work?

myDateTime.ToString("MMM. d, yyyy hh:mmtt \C\S\T");


来源:https://stackoverflow.com/questions/448634/how-to-format-a-datetime-like-oct-10-2008-1043am-cst-in-c-sharp

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