C# – How to list all month names, e.g. for a combo


At the moment I'm creating a DateTime for each month and formatting it to only include the month.
Is there another or any better way to do this?

Best Solution

You can use the DateTimeFormatInfo to get that information:

// Will return January
string name = DateTimeFormatInfo.CurrentInfo.GetMonthName(1);

or to get all names:

string[] names = DateTimeFormatInfo.CurrentInfo.MonthNames;

You can also instantiate a new DateTimeFormatInfo based on a CultureInfo with DateTimeFormatInfo.GetInstance or you can use the current culture's CultureInfo.DateTimeFormat property.

var dateFormatInfo = CultureInfo.GetCultureInfo("en-GB").DateTimeFormat;

Keep in mind that calendars in .Net support up to 13 months, thus you will get an extra empty string at the end for calendars with only 12 months (such as those found in en-US or fr for example).