views:

362

answers:

4

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  A: 

Assuming your server is configured to CST:

string format = dateTime.ToString("mmm. dd, YYYY HH:MM tt ")
    .Replace(" AM ", "am")
    .Replace(" PM ", "pm") +
    " CST";
Trent
close, but there's no space before AM or PM
Joel Coehoorn
+3  A: 
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

Jobo
+10  A: 

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);
John Sheehan
Here's a formatting cheat sheet: http://john-sheehan.com/blog/net-cheat-sheets/
John Sheehan
A: 

Will this work?

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