Get month name from two digit month number

后端 未结 4 1008
既然无缘
既然无缘 2021-02-02 09:26

I want to get month name from two digit month number (ex- 09). I tried with this code. But it doesn\'t work. The code give current month name only. What are the correct code for

相关标签:
4条回答
  • 2021-02-02 10:06

    You need to pass the month as a number, not text - so...

    var formattedMonth = moment().month(9).format('MMMM');
    console.log(formattedMonth)
    

    Result: October

    0 讨论(0)
  • 2021-02-02 10:17

    For those looking to do it and changing languages (locale), this is what I did

    let month = moment().month(09).locale('pt-br').format('MMMM');
    
    0 讨论(0)
  • 2021-02-02 10:20

    You want to pass the month when you create the Moment object:

    var formattedMonth = moment('09', 'MM').format('MMMM'); // September
    
    moment(
        '09',           // Desired month
        'MM'            // Tells MomentJs the number is a reference to month
    ).format('MMMM')    // Formats month as name
    
    0 讨论(0)
  • 2021-02-02 10:23

    While there's nothing wrong with Kevin's answer, it is probably more correct (in terms of efficiency) to obtain the month string without going through a moment object.

    var monthNum = 9;   // assuming Jan = 1
    var monthName = moment.months(monthNum - 1);      // "September"
    var shortName = moment.monthsShort(monthNum - 1); // "Sep"
    
    0 讨论(0)
提交回复
热议问题