Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a date as localized Short MonthDay string

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

like image 465
Wouter Avatar asked May 07 '10 08:05

Wouter


1 Answers

You could take the MonthDay pattern and replace "MMMM" with "MMM" - then apply that pattern:

string pattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
pattern = pattern.Replace("MMMM", "MMM");
string formatted = dateTime.ToString(pattern);

It's somewhat crude, but I believe it would work.

like image 115
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet