How do you get the month name from Date using JavaScript?

Let’s have a look at how you can achieve it. Let’s create a date object using the current date time in JavaScript.

let currentDateTime = new Date();
console.log('current date is ', currentDateTime);
// Output : Thu Nov 19 2020 16:23:58 GMT+0530 (India Standard Time)

You can get the current month as a value using the inbuilt getMonth method on a Date object.

let monthValue = currentDateTime.getMonth();
console.log('Month is ', monthValue);
// Output : 10

Months starting from January to December are being assigned a value from 0 to 11 respectively. So, to get the Month name you can create a custom method that outputs the month name based on the month value obtained from the Date object.

function getMonthName(val){
  switch(val){
    case 0 : 
    	return 'January';
    case 1 : 
    	return 'February';
    case 2 : 
    	return 'March';
    case 3 : 
    	return 'April';
    case 4 : 
    	return 'May';
    case 5 : 
    	return 'June';
    case 6 : 
    	return 'July';
    case 7 : 
    	return 'August';
    case 8 : 
    	return 'September';
    case 9 : 
    	return 'October';
    case 10 : 
    	return 'November';
    case 11 : 
    	return 'December';
    default:
    	return '';
  }
}

Using the above method you will be able to get the month name from a date object.

let monthValue = currentDateTime.getMonth();
console.log('Month Name is ', getMonthName(monthValue);
// Output : Month Name is November