Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get month name from two digit month number

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 it?

 var formattedMonth = moment().month('09').format('MMMM'); 
like image 390
GRTZ Avatar asked Jul 07 '15 16:07

GRTZ


People also ask

How do I convert month number to month name?

An alternative way to get a month number from an Excel date is using the TEXT function: =TEXT(A2, "m") - returns a month number without a leading zero, as 1 - 12. =TEXT(A2,"mm") - returns a month number with a leading zero, as 01 - 12.

How do I get month and date of JavaScript in 2 digit format?

Use the getMonth() method to get the month for the given date. Use the getDate() method to get the day of the month for the given date. Use the padStart() method to get the values in a 2-digit format.


2 Answers

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" 
like image 98
Matt Johnson-Pint Avatar answered Sep 21 '22 10:09

Matt Johnson-Pint


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 
like image 31
Kevin Boucher Avatar answered Sep 20 '22 10:09

Kevin Boucher