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.

4 Answers4

10

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
  • 77,456
  • 30
  • 160
  • 194
3
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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Jobo
  • 940
  • 6
  • 11
1

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
  • 560
  • 4
  • 11
0

Will this work?

myDateTime.ToString("MMM. d, yyyy hh:mmtt \C\S\T");
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147