In this article I’ll be explaining how to display the name of a month using the date() function. This is a simple logic where I use the Month number and an array with month names stored in it. The month number will be used as the index number for traversing the array. For now lets take the current month.
// Get the current date
let currentDate = newDate();
let monthIndex = currentDate.getMonth();
// Array of month names
let monthNames = [ ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’ ];
// Get the current month’s name
let currentMonthName = monthNames[monthIndex];
Now the variable “currentMonthName” contains the name of the current month as a string in it. Using this variable the month name can be displayed.