Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

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 setUTCMonth() function of the date object accepts an integer representing the month of the year and replaces the value of the month in the current date with it according to the universal time (0 represents January and so on...).

Syntax

Its Syntax is as follows

dateObj.setUTCMonth();

Example

Live Demo

<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <script type="text/javascript">
         var dateObj = new Date('september 26, 89 12:4:25:96');
         document.write("Current date:  "+dateObj.toUTCString());
         document.write("<br>");
         dateObj.setUTCMonth(6);        
         document.write("Date after setting new month value: "+dateObj.toUTCString());
      </script>
   </body>
</html>

Output

Current date: Tue, 26 Sep 1989 06:34:25 GMT
Date after setting new month value: Wed, 26 Jul 1989 06:34:25 GMT 

Example

Though you do not mention the minutes of the day while creating the date object, You can still set it using the setUTCMonth() function.

Live Demo

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

Output

Fri Sep 01 1989 05:04:25 GMT+0530 (India Standard Time)

Example

In the same way, though you do not pass any value to the constructor while creating the date object still you can set the setUTCMonth() using this function and the date, year and, other values remain same as in the current date (and time).

Live Demo

<html>
   <head>
      <title>JavaScript Example</title>
   </head>
   <body>
      <script type="text/javascript">
         var dateObj = new Date();
         dateObj.setUTCMonth(8);        
         document.write(dateObj.toString());
      </script>
   </body>
</html>

Output

Tue Sep 18 2018 22:13:48 GMT+0530 (India Standard Time)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.