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

后端 未结 16 1019
栀梦
栀梦 2020-12-23 14:38

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?

16条回答
  •  悲哀的现实
    2020-12-23 15:14

    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;
    

提交回复
热议问题