JavaScript getMonth() | Get the Month of Date

The JavaScript getMonth() method is used when we need to get the month (0-11) of a date. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let mon = d.getMonth();
      document.getElementById("xyz").innerHTML = mon;
   </script>

</body>
</html>
Output

JavaScript getMonth() Syntax

The syntax of getMonth() method in JavaScript is:

x.getMonth()

where x must be an object of the Date() constructor.

The getMonth() method returns a number from 0 to 11 where:

  • 0 is for January
  • 1 is for February
  • 2 is for March
  • 3 is for April
  • 4 is for May
  • 5 is for June
  • 6 is for July
  • 7 is for August
  • 8 is for September
  • 9 is for October
  • 10 is for November
  • 11 is for December

Get Name of the Current Month in JavaScript

Let me create an example to print the name of the current month instead of the number.

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p>Month: <span id="res"></span></p>

   <script>
      const d = new Date();
      let mon = d.getMonth();
      if(mon==0)
         document.getElementById("res").innerHTML = "January";
      else if(mon==1)
         document.getElementById("res").innerHTML = "February";
      else if(mon==2)
         document.getElementById("res").innerHTML = "March";
      else if(mon==3)
         document.getElementById("res").innerHTML = "April";
      else if(mon==4)
         document.getElementById("res").innerHTML = "May";
      else if(mon==5)
         document.getElementById("res").innerHTML = "June";
      else if(mon==6)
         document.getElementById("res").innerHTML = "July";
      else if(mon==7)
         document.getElementById("res").innerHTML = "August";
      else if(mon==8)
         document.getElementById("res").innerHTML = "September";
      else if(mon==9)
         document.getElementById("res").innerHTML = "October";
      else if(mon==10)
         document.getElementById("res").innerHTML = "November";
      else if(mon==11)
         document.getElementById("res").innerHTML = "December";
   </script>

</body>
</html>
Output

Month:

The same example can also be written as:

<!DOCTYPE html>
<html>
<body>
   
   <p>Month: <span id="res"></span></p>

   <script>
      const d = new Date();
      let mon = d.getMonth();
      const monthList = ["January", "February", "March", "April",
         "May", "June", "July", "August", "September", "October",
         "November", "December"];
      document.getElementById("res").innerHTML = monthList[mon];
   </script>

</body>
</html>

Get First Three Letters of the Month in JavaScript

To print only the first three letters of the month, either replace all the month names with its first three letter alternatives, that is:

const monthList = ["Jan", "Feb", "Mar", "Apr",
   "May", "Jun", "Jul", "Aug", "Sep", "Oct",
   "Nov", "Dec"];

Or follow the following example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p>Month: <span id="res"></span></p>

   <script>
      const d = new Date();
      const monthList = ["January", "February", "March", "April",
         "May", "June", "July", "August", "September", "October",
         "November", "December"];
      let monThr = monthList[d.getMonth()];
      document.getElementById("res").innerHTML = monThr.slice(0, 3);
   </script>

</body>
</html>
Output

Month:

Please note: The slice() method slice required part of a defined string. For example: myString.slice(0, 3) slice all the characters from the string, from index number 0 (start) to index number 2 where the characters at both the indexes are included.

Please note: To display date in format dd-mm-yyyy, refer to its separate example.

Please note: To display time in format hh:mm:ss, refer to its separate example.

Please note: To display time in format hh:mm:ss AM/PM, refer to its separate example.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋