I’m to get a custom DateTime format including the AM/PM designator, but I want the ‘AM’ or ‘PM’ to be lowercase without making the rest of of the characters lowercase.
Is this possible using a single format and without using a regex?
Here’s what I’ve got right now:
item.PostedOn.ToString('dddd, MMMM d, yyyy a\\t h:mmtt')
An example of the output right now would be Saturday, January 31, 2009 at 1:34PM
I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower:
Another option (which I’ll investigate in a sec) is to grab the current DateTimeFormatInfo, create a copy, and set the am/pm designators to the lower case version. Then use that format info for the normal formatting. You’d want to cache the DateTimeFormatInfo, obviously…
EDIT: Despite my comment, I’ve written the caching bit anyway. It probably won’t be faster than the code above (as it involves a lock and a dictionary lookup) but it does make the calling code simpler:
Here’s a complete program to demonstrate: