JavaScript Get Month Name With Various Examples

JavaScript Get Month Name With Various Examples

Get month name in javascript with several examples. In this tutorial we will explain, how to get month name in javascript, get month name from date string in javascript, get previous month name in javascript, convert month number to month name in javascript and how to get month name from number in javascript.

JavaScript Get Month Name

Here we will take e.g. like, javascript get month name from date, javascript get month from date string, javascript get next month name, javascript get text month from date, javascript get previous month name, convert month number to month name using javascript.

In this tutorial, we will create a javascript function, which is used to return month name by date string, number, the current date and convert month number to month name in javascript.

1. javascript get current month name

Now we will create a javascript function, which is used to get the current month name in a string.

   function getCurrentMonthName(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()];
   };

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get current date month name</title>
</head>
<body>
  <script type = "text/javascript">

   function getCurrentMonthName(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()];
   };

    var date = new Date(); // date object
    var mName = getCurrentMonthName(date); // get month current month name
    document.write( "javascript get current date month name :- " + mName ); 

  </script>  
</body>
</html>

Result of the above code is:

javascript get current date month name

2. javascript get month from date string

Here, we will create function, which is used to return the month name from given date string in javascript:

   function getMonthNameDateString(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()];
   };

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get month from specific date string</title>
</head>
<body>
  <script type = "text/javascript">

   function getMonthNameDateString(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()];
   };

    var date = new Date("Jan 26 2019 10:32:18"); // date object
    var mName = getMonthNameDateString(date); // get month from specific date string
    document.write( "javascript get month from specific date string :- " + mName ); 

  </script>  
</body>
</html>

Result of the above code is:

javascript get month from specific date string

3. javascript get previous month name

Here we will create a function that is used to get the previous month name:

   function getPreviousMonthName(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()-1];
   };

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get previous month name</title>
</head>
<body>
  <script type = "text/javascript">

   function getPreviousMonthName(dt){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[dt.getMonth()-1];
   };

    var date = new Date(); // date object
    var mName = getPreviousMonthName(date); // javascript get previous month name
    document.write( "javascript get previous month name :- " + mName ); 

  </script>  
</body>
</html>

Result of the above code is:

javascript get previous month name

4. get month name from number in javascript

Here we will create a function for getting a month name from number in javascript. In this function, you will pass the month number between 0 to 11 and this function converts month number to month name using javascript.

Note 0 is jan, 1 is feb, 2 is march and so on.

   function getMonthNameNumber(number){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[number.getMonth()];
   };

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>get month name from number in javascript</title>
</head>
<body>
  <script type = "text/javascript">

   function getMonthNameNumber(number){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[number];
   };

    var mName = getMonthNameNumber(5); // converts month number to month name using javascript
    document.write( "get month name from number in javascript :- " + mName ); 

  </script>  
</body>
</html>

Result of the above code is:

get month name from number in javascript

5. javascript get next month name

Here we will create a function that is used to return next month name using current date.

   function getNextMonthName(date){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[date.getMonth()+1];
   };

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get next month name</title>
</head>
<body>
  <script type = "text/javascript">

   function getNextMonthName(date){
     monthNamelist = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    return monthNamelist[date.getMonth()+1];
   };

    var date = new Date(); // date object
    var mName = getNextMonthName(date); // get next month in two digits
    document.write( "javascript get next month name :- " + mName ); 

  </script>  
</body>
</html>

Result of the above code is:

javascript get next month name

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. Get Month 2 Digits in JavaScript
  2. javaScript Get Current Year 2 and 4 Digit – Example
  3. Get Current Date Time javaScript
  4. JavaScript: Date setUTCDate() Method
  5. Set UTC Month In javaScript
  6. setUTCFullYear() Method JavaScript With Examples
  7. javascript date setUTCHours() Method With Examples
  8. JavaScript: d.setUTCMinutes() Method e.g.
  9. setUTCSecond() Method By JavaScript
  10. javaScript Date set UTC milliseconds
  11. setUTCSecond() Method By JavaScript
  12. JavaScript: Date.getUTCDate() Method
  13. getUTCmonth Method javaScript With Example
  14. Digital Clock with date in javaScript
  15. JavaScript: Set Date Methods
  16. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

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