abatishchev
abatishchev

Reputation: 100366

Fill combobox with english months

I have to fill a combobox with month in English: January, February, etc. I did it next way:

private string[] AmericanMonths
{
  get
  {
    var arr = new string[12];
    var americanCulture = new CultureInfo("en-US");
    for (int i = 0; i < 12; i++)
    {
      arr[i] = new DateTime(2000, i + 1, 1).ToString("MMMM", americanCulture);
    }
    return arr;
  }
}

comboMonth.Items.AddRange(AmericanMonths);

How do you think, what way is better (perfomance)?

Is there a way to convert an integer representing month number to it's name?

Upvotes: 4

Views: 6207

Answers (2)

Jose Basilio
Jose Basilio

Reputation: 51548

The CultureInfo already contains the list of month names right in the framework. You could just write:

var americanCulture = new CultureInfo("en-US");
comboMonth.Items.AddRange(americanCulture.DateTimeFormat.MonthNames.Take(12)); //to remove last empty element

Upvotes: 18

Mikko Rantanen
Mikko Rantanen

Reputation: 8084

CultureInfo has DateTimeFormat property which contains the information about the date time formatting. From there you can use GetMonthName or GetAbbreviatedMonthName. Or alternatively you can get the array of month names through the MonthNames property of the DateTimeFormatInfo.

Upvotes: 2

Related Questions