如何在c#中获取月份的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1678771/
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-06 19:56:17  来源:igfitidea点击:

How to get an array of months in c#

c#

提问by LIX

I want to get the month array in c#.
somthing like this : { January , February , ... , December }
How can I do this? please send me codes in C#. thanks

我想在 c# 中获取月份数组。
类似这样的事情:{ January , February , ... , December }
我该怎么做?请给我发送 C# 代码。谢谢

采纳答案by nmdr

You need to careful about localization issues as well: You can use:

您还需要注意本地化问题:您可以使用:

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

The genitive case is introduced in some inflected languages by a genitive noun inflection, which in non-inflected languages matches the use of the equivalent of the English preposition "of". For example, a date in the Russian (Russia), "ru-RU", culture, consists of the day number and the genitive month name.

属格格在一些屈折语言中由属格名词屈折引入,在非屈折语言中,它与英语介词“of”等价物的使用相匹配。例如,俄语(俄罗斯)“ru-RU”文化中的日期由日数和属格月份名称组成。

More info…

更多信息…

EDIT: If you need english month names you can set your current culture as en-US

编辑:如果您需要英文月份名称,您可以将当前文化设置为en-US

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 

回答by Wael Dalloul

string[] months = new string[] {"January", "February", "March", "April", "May",
  "June", "July", "August", "September", "October", "November", "December"};

回答by o.k.w

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;

回答by WhiteleyJ

An alternate that hopefully teaches how you can apply the range function to create for sequential things.

一个替代方案,希望能教你如何应用范围函数来创建顺序事物。

var startDate = new DateTime(2014,1,1);
var months = Enumerable.Range(0,11)
                       .Select(startDate.AddMonths);
                       .Select(m => m.ToString("yyyy MMMM"))
                       .ToList();

What it is doing is creating a DateTime object (startDate) and using that object to generate all the other dates relative to itself.

它正在做的是创建一个 DateTime 对象 (startDate) 并使用该对象生成与其自身相关的所有其他日期。

  1. Enumerable.Range(0,11) creates a list of integers {0,1,2,3,4,5,6,7,8,9,10,11}

  2. Select(startDate.AddMonths) feeds each of those integers to the AddMonths function of startDate which produces a list of dates from January to December.

  3. Select(m => m.ToString("yyyy MMMM") takes each of the dates from january to december and converts them into a formatted string (in this case "2014 January")

  4. ToList() evaluates all the functions and returns it as a list of strings.

  1. Enumerable.Range(0,11) 创建一个整数列表 {0,1,2,3,4,5,6,7,8,9,10,11}

  2. Select(startDate.AddMonths) 将这些整数中的每一个提供给 startDate 的 AddMonths 函数,该函数生成从一月到十二月的日期列表。

  3. Select(m => m.ToString("yyyy MMMM") 获取从 1 月到 12 月的每个日期并将它们转换为格式化字符串(在本例中为“2014 年 1 月”)

  4. ToList() 评估所有函数并将其作为字符串列表返回。