问题
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