How to list all month names, e.g. for a combo?
Solution 1
You can use the DateTimeFormatInfo
to get that information:
// Will return January
string name = DateTimeFormatInfo.CurrentInfo.GetMonthName(1);
or to get all names:
string[] names = DateTimeFormatInfo.CurrentInfo.MonthNames;
You can also instantiate a new DateTimeFormatInfo
based on a CultureInfo
with DateTimeFormatInfo.GetInstance
or you can use the current culture's CultureInfo.DateTimeFormat
property.
var dateFormatInfo = CultureInfo.GetCultureInfo("en-GB").DateTimeFormat;
Keep in mind that calendars in .Net support up to 13 months, thus you will get an extra empty string at the end for calendars with only 12 months (such as those found in en-US or fr for example).
Solution 2
This method will allow you to apply a list of key value pairs of months to their int
counterparts. We generate it with a single line using Enumerable Ranges and LINQ. Hooray, LINQ code-golfing!
var months = Enumerable.Range(1, 12).Select(i => new { I = i, M = DateTimeFormatInfo.CurrentInfo.GetMonthName(i) });
To apply it to an ASP dropdown list:
// <asp:DropDownList runat="server" ID="ddlMonths" />
ddlMonths.DataSource = months;
ddlMonths.DataTextField = "M";
ddlMonths.DataValueField = "I";
ddlMonths.DataBind();
Solution 3
You can use the following to return an array of string containing the month names
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
Solution 4
They're defined as an array in the Globalization namespaces.
using System.Globalization;
for (int i = 0; i < 12; i++) {
Console.WriteLine(CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);
}
Solution 5
public IEnumerable<SelectListItem> Months
{
get
{
return Enumerable.Range(1, 12).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(x)
});
}
}
Related videos on Youtube
ProfK
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET, with SQL Server. I have, in some way or another, been involved in software development for about eighteen years, but always learning something new. At the moment that is WPF and MVVM.
Updated on July 05, 2022Comments
-
ProfK almost 2 years
At the moment I'm creating a
DateTime
for each month and formatting it to only include the month.
Is there another or any better way to do this? -
Greg over 15 yearsFor some reason the code I grabbed was using the Invariant name for the ListItem.Value property. Not sure why, but you might want to use an integer for that instead.
-
Sarawut Positwinyu over 12 yearsI use CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName
-
The Muffin Man over 11 yearsYour second example won't compile. You need to create a new instance of
DateTimeFormatInfo
or call itsGetInstance()
method from the static class .e.g.new DateTimeFormatInfo().MonthNames;
orDateTimeFormatInfo.GetInstance().MonthNames
-
Sam almost 11 yearsI've corrected the issues noted by the comments (in the future you can just edit the post).
-
John over 9 yearsLike the use of Enumerable.Range , I'm going to use this with a List of MVC SelectListItems
-
RoLYroLLs about 6 yearsup'd for that
Where
clause -
CompuChip over 5 yearsNote: I had to use the answer with the
Enumerable.Range
becauseDateTimeFormatInfo.MonthNames
returns an array with 13 elements so if you.Select
on that you get an empty item at the end. -
ProfK over 5 yearsVery nice LINQ.
-
Andrew Truckle over 3 yearsThanks! TakeWhile was just what I needed. :)