Setting AM & PM Designators

Posted within Development on

This is a useful little tip. By default, AM and PM time designators are output in uppercase (in the 1033 language set atleast) but there are times (in this case, enforced by a coporate style guide) when they should be lowercase. There are two ways to achieve this – one is…

DateTime now = DateTime.Now;
String nowString = now.ToString("dddd, d MMMM yyyy a\\t");
nowString = nowString + now.ToString("tt").ToLower();

… which is okay if you’re just using it the once. More than once, you’re better off using a DateTimeFormatInfo to set the AM and PM designators. Like so…

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.AMDesignator = "am";
dtfi.PMDesignator = "pm";

String nowString = now.ToString("dddd, d MMMM yyyy a\\t h.mmtt", dtfi);