Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 210
abatishchev Avatar asked Nov 30 '22 12:11

abatishchev


1 Answers

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
like image 63
Jose Basilio Avatar answered Dec 05 '22 20:12

Jose Basilio