Below is the example of Date setSeconds() method.
// Here a date has been assigned according to
// universal time while creating Date object
'October 13, 1996 05:35:32 GMT-3:00'
// New month of January is being set in above Date
// Object with the help of setUTCMonth() method
// New month from above Date Object is
// being extracted using getUTCMonth()
B = dateobj.getMonth();
// Printing new month
The date.setUTCMonth() method is used to set month according to universal time into a date object which are created using date() constructor.
Parameter: This method accept a single parameter as mentioned above and described below:
- month_Value This parameter holds the value of month which is used to set in date() constructor.
Return values: It returns the new i.e updated month which is set by setUTCMonth() method.
Note: The dateObj is a valid Date object created using Date() constructor in which we want to set the month.
More codes for the above method are as follows:
Program 1: If in Date() constructor we do not give any month, still setUTCMonth() method set new month which is given as its parameter.
Program 2: If nothing as parameter is given in Date() constructor, still setUTCMonth() method set month but year, date etc become current ones according to universal time.
Here 11 is the new month of December, 1 is the current date and 2018 is the current year according to universal time.
11 1 2018
Program 3: If value of month 15 is given as the parameter of setUTCMonth() method, It will set 3 as the month because month range is from 0 to 11 and hence , here 12 is subtracted because 0 to 11 is 12.
Here 3 is the new month of April and the year becomes 1997 from 1996 because the month range is from 0 to 11 i.e, total 12 and we set new month as 3 which increases year by one to 1997 from 1996 and the month becomes 3 according to universal time.
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.