How to format DateTime as "October 10, 2008 10:43 AM CST" in C #

Is there a clean way to format the DateTime value as "October 10, 2008 10:43 AM CST".

I need this with the corresponding abbreviations and am (or pm) in lowercase, etc. etc.

I did it myself, but it's ugly, so I'm looking for another activity.

Thank.

+4
c # datetime formatting
Jan 15 '09 at 21:56
source share
4 answers

Since the "tt" format string specifier displays only uppercase, you will have to change this yourself. In addition, DateTimes does not save the time zone name, but only the offset.

DateTime dt = DateTime.Now; string ampm = dt.ToString("tt").ToLower(); string output = string.Format("{0:MMM. d, yyyy h:mm}{1}", dt, ampm); 
+10
Jan 15 '09 at 22:05
source share
 DateTimeObject.ToString("MMM. dd, yyyy hh:mmtt"); 

not sure about CST.

If you need more combinations, see this link:

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

+3
Jan 15 '09 at 22:02
source share

Assuming your server is configured for CST:

 string format = dateTime.ToString("mmm. dd, YYYY HH:MM tt ") .Replace(" AM ", "am") .Replace(" PM ", "pm") + " CST"; 
+1
Jan 15 '09 at 21:58
source share

Will this work?

 myDateTime.ToString("MMM. d, yyyy hh:mmtt \C\S\T"); 
0
Jan 15 '09 at 22:08
source share



All Articles