How to Get Current, Next and Previous Month Name from Date using JavaScript

How to Get Current, Next and Previous Month Name from Date using JavaScript

If you are working with date in JavaScript and you want to get the current month’s name, the previous month’s name, and the next month’s name from the date. what will you do then Don’t worry there are many methods in JavaScript for this. what you will learn from this tutorial

Through this tutorial, you will learn how to get the current, next and previous month names from a given date in JavaScript.

This tutorial will show you many examples like javascript to 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.

How to Get Current, Next and Previous Month Name from Date using JavaScript

By using the following ways, you can get the current, previous, and next month’s names from the date in JavaScript:

  • Get the current month name using getMonth()
  • JavaScript gets month-from-date string
  • JavaScript get the previous month’s name using getMonth()
  • Get the month name from the number in javascript using the number.getMonth()
  • JavaScript get next month’s name using date.getMonth()+1

Get the current month name using getMonth()

You can use the following method, 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()];
   };

Here is an example get the current month name using getMonth(); is as follows:

<!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>

JavaScript gets month-from-date string

You can use the getMonthNameDateString() function to get the month name from a given date string in javascript, which is given below:

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

Here is an example of getMonthNameDateString() function to get the month name from a given date string in javascript:

<!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>

JavaScript get the previous month’s name using getMonth()

If you want to get the previous month name, you need to use the following function, which is get previous month name in javaScript:

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

Here is an example of get the previous month name in JavaScript:

<!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>

Get the month name from the number in javascript using number.getMonth()

In this way, you 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()];
   };

Here is an example:

<!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>

JavaScript get next month name using date.getMonth()+1

If you want to get next month name, you can use date.getMonth()+1 to get next month name from date in javaScript:

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

Here is an example to get next month’s name in JavaScript:

<!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>

Conclusion

In this tutorial, you have learned how to get current, next and previous month name from date in javaScript.

Recommended JavaScript Tutorials

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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