Get a culture specific list of month names
Posted
by erwin21
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by erwin21
Published on Tue, 11 May 2010 08:31:47 GMT
Indexed on
2010/05/11
8:34 UTC
Read the original article
Hit count: 330
A while ago I found a clever way to retrieve a dynamic culture specific list of months names in C# with LINQ.
1: var months = Enumerable.Range(1, 12)
2: .Select(i => new
3: {
4: Month = i.ToString(),
5: MonthName = new DateTime(1, i, 1).ToString("MMMM")
6: })
7: .ToList();
It’s fairly simple, for a range of numbers from 1 to 12 a DateTime object is created (year and day doesn’t matter in this case), then the date time object formatted to a full month name with ToString(“MMMM”). 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.
© ASP.net Weblogs or respective owner