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

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

    How to create a custom list of month names in any order

    Yes, I'm answering a question from over 10 years ago! :D

    Yet, I wanted to add this code snippet on the chance it might help others. It shows how to output a list of month names in any custom order. In my case I needed it to start in October, but you could put the months in any sequence (even have repeating months) by setting the list of integers.

    model.Controls = new
    {
        FiscalMonths = new
        {
            Value = DateTime.Now.Month,
            Options = (new List { 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9 }).Select(p => new
            {
                Value = p,
                Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(p)
            })
        }
    };
    

    And the json output for use in a dropdown:

      "FiscalMonths": {
       "Value": 10,
       "Options": [
        {
         "Value": 10,
         "Text": "October"
        },
        {
         "Value": 11,
         "Text": "November"
        },
        {
         "Value": 12,
         "Text": "December"
        },
        {
         "Value": 1,
         "Text": "January"
        },
        {
         "Value": 2,
         "Text": "February"
        },
        etc ....
    

提交回复
热议问题