Get a culture specific list of month names
- by erwin21
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();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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.