• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Convert Month Number to Name

Using JavaScript to Convert Month Number to Name

October 10, 2022 Leave a Comment

We can use JavaScript to convert a month number to a name easily by making use of an array of month names. To convert a month number to a name, we simply have to create an array of month names, which we will call months. We will then match the given month number to the month name.


var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthNumber = 8;
var monthName = months[monthNumber-1];

We can put this code in a function, so all we need is to enter in the month number and the month name will be returned. We will call the function monthNameFromNumber().

function monthNameFromNumber(num){
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthNumber = Number(num);
  var monthName = months[monthNumber-1];
  return monthName;
}

Now let’s see our function in use with a couple of examples.

function monthNameFromNumber(num){
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthNumber = Number(num);
  var monthName = months[monthNumber-1];
  return monthName;
}

console.log(monthNameFromNumber(1));
console.log(monthNameFromNumber(05));
console.log(monthNameFromNumber(12));

#Output
January
May
December

Let’s take a look at another way to get the month name from a number.

Get the Current Month Name From a Number

To start, we can get the current month of the year by using the getMonth() method. It will return a number from 0 to 11, with 0 corresponding to January, and 11 to December.

We can convert the month number we get from the getMonth() method into the month as a String. To do this we just need to make an array with all the months as seen below.

function getCurrentMonthName(){
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
}

We can then use the getMonth() value to find the correct month in the array.

function getCurrentMonthName(){
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var currDate = new Date();
  var currMonth = currDate.getMonth();
  return months[currMonth];
}

console.log(getCurrentMonthName());

#Output
October

The value returned by our function getCurrentMonthName() in the code above will be whatever month of the year it currently is, which in this case would be October.

Hopefully this article has been useful in showing how to use JavaScript to convert month number to name.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 2.  JavaScript substring – How to Get a Substring From a String
  • 3.  JavaScript Infinite Loop
  • 4.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 5.  Using JavaScript to Remove Backslash From a String
  • 6.  Using JavaScript to Check if String Contains Only Numbers
  • 7.  Using JavaScript to Change Text of Element
  • 8.  Using JavaScript to Get the Scroll Position
  • 9.  Using JavaScript to Resize an Image
  • 10.  Using JavaScript to Get Today’s Date

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy