JavaScript getUTCMonth() | Get the Month of UTC Date

The JavaScript getUTCMonth() method is used to get the month of the UTC date. UTC stands for Universal Time Coordinated which is same as Greenwich Mean Time (GMT). For example:

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

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

</body>
</html>
Output

JavaScript getUTCMonth() Syntax

The syntax of getUTCMonth() method in JavaScript is:

x.getUTCMonth()

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

The getUTCMonth() 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 based on UTC Date in JavaScript

Let me create an example to print the name of the current month (based on the UTC date) 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.getUTCMonth();
      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.getUTCMonth();
      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 UTC Month in JavaScript

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.getUTCMonth()];
      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 (included) to index number 2 (included).

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋