skip to Main Content

I have to display month number instead of it’s name
i am using type=’month’ in html.helper

 @Html.EditorFor(model => model.DueMonth, new
                        {
                            htmlAttributes = new
                            {
                                @class = "form-control",
                                type = "month",
                                @Value = Model.DueMonth!= null ? 
                                 Model.DueMonth.Date.ToString("yyyy-MM") : null
                            }
                        })

**
but it is displaying
March 2023
i have to display 03 2023**

2

Answers


  1. Instead of "yyyy-MM" use "MM yyyy".

    Like this

    @Value = Model.DueMonth != null ? Model.DueMonth.Date.ToString("MM yyyy") : null
    
    Login or Signup to reply.
  2. The UI provided by the input type="date" is controlled by the browser (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month). If you want to control the format, you can do one of three things:

    1. Change the input type to text
    2. Write some JavaScript/CSS to mask the display part of the control with your own version
    3. Find a third party component that provides what you need.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search