JavaScript setUTCMonth() Method

JavaScript: Date.setUTCMonth() method; Through this tutorial, i am going to show you how to set UTC month in javascript with the help of examples.

JavaScript Date.setUTCMonth() method

Javascript date setUTCMonth() method, which is used to sets the month for the specified date according to universal time. The setUTCMonth method will accept an integer value between 0 and 11.

Note: January is 0, February is 1, and so on.

Syntax of JavaScript Date.setUTCMonth() method

The syntax is date.setUTCMonth method is:

 Date.setUTCMonth(month, day);

Parameters of JavaScript Date.setUTCMonth() method

ParameterDescription
month Required. An integer representing the month.

Expected values are 0-11, but other values are allowed:
1. -1 will result in the last month of the previous year
2. 12 will result in the first month of the next year
3. 13 will result in the second month of the next year
dayOptional. An integer representing the day of month.

Expected values are 1-31, but other values are allowed:
1. 0 will result in the last hour of the previous month
2. -1 will result in the hour before the last hour of the previous month

If the month has 31 days:
32 will result in the first day of the next month
If the month has 30 days:
32 will result in the second day of the next month

Example 1 – JavaScript Date.setUTCMonth() method

Let’s take first example using the setUTCMonth javascript mothod; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript date.setUTCMonth Method</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date();
     date.setUTCMonth( 5 );
     document.write( "The javascript date.setUTCMonth() : " + date );
  </script>  
</body>
</html>

Output of the above code is:

JavaScript date.setUTCMonth Method

Example 2 – JavaScript Date.setUTCMonth() method

Let’s take an second example using setUTCMonth(); as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript date.setUTCMonth - Set the month to 5 (June) and the day to the 15th</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date();
     date.setUTCMonth(5, 15);
     document.write( "Set the month to 5 (June) and the day to the 15th using date.setUTCMonth() : " + date );
  </script>  
</body>
</html>

Output of the above code is

JavaScript date.setUTCMonth – Set the month to 5 (June) and the day to the 15th

Conclusion

In this javascript tutorial, you have learned how to set a month and day of month using setUTCMonth function with example.

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

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