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 the 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 the 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:

Get the name of the current month based on the 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 the 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 requires part of a defined string. For example, myString.slice(0, 3) slices all the characters from the string from index number 0 (included) to index number 2 (included).

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!