Get a culture specific list of month names
A while ago I found a clever way to retrieve a dynamic culture specific list of month names in C# with LINQ.
1: var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
2: .TakeWhile(m => m != String.Empty)
3: .Select((m,i) => new
4: {
5: Month = i+1,
6: MonthName = m
7: })
8: .ToList();
It’s fairly simple, from the current culture a list of full month names is retrieved (if you want the abbreviated name of the specified month you can use AbbreviatedMonthNames property). We use the method TakeWhile because the MonthNames array contains a empty 13th month.
In this example an anonymous object is created with a Month and MonthName property.
You can use this solution to populate your dropdown list with months or to display a user friendly month name.
Thanks to CW to point me to MonthNames property.