C# 如何列出所有月份的名称,例如组合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/315301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:52:53  来源:igfitidea点击:

How to list all month names, e.g. for a combo?

c#asp.net.netdatetime

提问by ProfK

At the moment I'm creating a DateTimefor each month and formatting it to only include the month.
Is there another or any better way to do this?

目前我正在DateTime为每个月创建一个并将其格式化为仅包含月份。
有没有其他或更好的方法来做到这一点?

采纳答案by Yona

You can use the DateTimeFormatInfoto get that information:

您可以使用DateTimeFormatInfo来获取该信息:

// 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 DateTimeFormatInfobased on a CultureInfowith DateTimeFormatInfo.GetInstanceor you can use the current culture's CultureInfo.DateTimeFormatproperty.

您还可以DateTimeFormatInfo基于CultureInfowith实例化一个新的,DateTimeFormatInfo.GetInstance或者您可以使用当前文化的CultureInfo.DateTimeFormat属性。

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).

请记住,.Net 中的日历最多支持 13 个月,因此对于只有 12 个月的日历(例如在 en-US 或 fr 中找到的日历),您将在末尾获得一个额外的空字符串。

回答by Rohan West

You can use the following to return an array of string containing the month names

您可以使用以下内容返回包含月份名称的字符串数组

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames

回答by Greg

You can get a list of localized months from Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNamesand invariant months from DateTimeFormatInfo.InvariantInfo.MonthNames.

您可以从 中获取本地化月份Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames和不变月份的列表DateTimeFormatInfo.InvariantInfo.MonthNames

string[] localizedMonths = Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames;
string[] invariantMonths = DateTimeFormatInfo.InvariantInfo.MonthNames;

for( int month = 0; month < 12; month++ )
{
    ListItem monthListItem = new ListItem( localizedMonths[month], invariantMonths[month] );
    monthsDropDown.Items.Add( monthListItem );
}

There might be some issue with the number of months in a year depending on the calendar type, but I've just assumed 12 months in this example.

根据日历类型的不同,一年中的月数可能存在一些问题,但在此示例中我只是假设了 12 个月。

回答by Zachary Yates

Try enumerating the month names:

尝试枚举月份名称:

for( int i = 1; i <= 12; i++ ){
  combo.Items.Add(CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[i]);
}

It's in the System.Globalization namespace.

它位于 System.Globalization 命名空间中。

Hope that helps!

希望有帮助!

回答by Dylan Beattie

They're defined as an array in the Globalization namespaces.

它们在 Globalization 命名空间中被定义为一个数组。

using System.Globalization;

for (int i = 0; i < 12; i++) {
   Console.WriteLine(CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);
}

回答by Jeevan Moses

List<string> mnt = new List<string>();    
int monthCount = Convert.ToInt32(cbYear.Text) == DateTime.Now.Year ? DateTime.Now.Month : 12;    
            for (int i = 0; i < monthCount; i++)    
            {    
                mnt.Add(CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);    
            }    
            cbMonth.DataSource = mnt;

回答by mmmeff

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!

此方法将允许您将月份的键值对列表应用于它们的 int 对应项。我们使用 Enumerable Ranges 和 LINQ 用一行生成它。万岁,LINQ 打码!

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 下拉列表:

// <asp:DropDownList runat="server" ID="ddlMonths" />
ddlMonths.DataSource = months;
ddlMonths.DataTextField = "M";
ddlMonths.DataValueField = "I";
ddlMonths.DataBind();

回答by WebDev07

public IEnumerable<SelectListItem> Months
{
  get
  {
    return Enumerable.Range(1, 12).Select(x => new SelectListItem
    {
      Value = x.ToString(),
      Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(x)
    });
  }
}

回答by Maher Ben Issa

A way to retrieve a dynamic culturespecific list of month names in C#with LINQ.

一种使用LINQC# 中检索特定于区域性动态月份名称列表的方法。

ComboBoxName.ItemsSource= 
System.Globalization.CultureInfo.
CurrentCulture.DateTimeFormat.MonthNames.
TakeWhile(m => m != String.Empty).ToList();

OR

或者

In this example an anonymous object is created with a Month and MonthName property

在此示例中,使用 Month 和 MonthName 属性创建了一个匿名对象

var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
 .TakeWhile(m => m != String.Empty)
 .Select((m,i) => new  
 {  
     Month = i+1,  
     MonthName = m
 }) 
 .ToList();

PS: We use the method TakeWhile because the MonthNames array contains a empty 13th month.

PS:我们使用 TakeWhile 方法是因为 MonthNames 数组包含一个空的第 13 个月。

回答by Nayeem Mansoori

string[] monthNames = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;

foreach (string m in monthNames) // writing out
{
    Console.WriteLine(m);
}

Output:

输出:

January
February
March
April
May
June
July
August
September
October
November
December

Update: Do note that for different locales/cultures, the output might not be in English. Haven't tested that before though.

更新:请注意,对于不同的语言环境/文化,输出可能不是英语。不过之前没测试过。

For US English only:

仅适用于美国英语:

string[] monthNames = (new System.Globalization.CultureInfo("en-US")).DateTimeFormat.MonthNames;