Computer >> Computer tutorials >  >> Programming >> Javascript

Date.getUTCMonth() function in JavaScript


The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The getUTCMonth() function of the Date object returns the month of the given date according to the universal time (0 represents January and so on...).

Syntax

Its Syntax is as follows

dateObj.getUTCMonth();

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('September 26, 1989 12:4:25:96');
      document.write(dateObj.getUTCMonth());
   </script>
</body>
</html>

Output

8

Example

Incase if you haven’t mentioned the month of the year while creating the date object, this function returns 0 according to universal time.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('1989 12:4:25:96');
      document.write(dateObj.getUTCMonth());
   </script>
</body>
</html>

Output

0

Example

In the same way if you haven’t passed anything while creating the date object, this function returns the Current month of the current year according to the universal time.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      document.write("Month of the year:+ "dateObj.getUTCMonth());
   </script>
</body>
</html>

Output

Month of the year: 9