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

后端 未结 16 930
栀梦
栀梦 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:16

    You can get a list of localized months from Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames and invariant months from 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.

提交回复
热议问题