I would like to format a DateTime to a string containing the month name abbreviated and the date for use in axis labels in a graph.
The default DateTime format strings do not contain abbreviated month. I guess there is no standard but I could take a substring of the first 3 characters of the month name and replace this into the MonthDay format. The reason I would use MonthDay is that the ordering of month and date is locale dependent.
Does anyone have a better idea?
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#MonthDay
You can pass a CultureInfo in the Date.ToString() method. Either you have a specific culture info or you can use the CultureInfo.CurrentCulture:
Quick, easy and does the job.
Edited: Use
date.ToString("d MMM")
. It will show 7 sep.You can use the custom string formatters to do this:
See the section "How Standard Format Strings Work" on the page you linked to for more info.
Here is the solution I came up with for the similar situation of wanting a localized version of
01/29
:Here is the format output for all cultures, so you can see if it will work for you:
The
DateMonth1
replace will remove an extra padding character for a couple cultures (specifically 'bg').You could take the
MonthDay
pattern and replace "MMMM" with "MMM" - then apply that pattern:It's somewhat crude, but I believe it would work.