The Date object is a data type 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 setFullYear() function of the date object accepts an integer representing the year and modifies/replaces the current year with it.
Syntax
Its Syntax is as follows
dateObj.setFullYear();
Example
<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.setFullYear(99); document.write("Date after setting the new year: "+dateObj.toUTCString()); </script> </body> </html>
Output
Current date: Tue, 26 Sep 1989 06:34:25 GMT Date after setting the new year: Sat, 26 Sep 2009 06:34:25 GMT
Example
Though you do not mention the year of the month while creating the date object, You can still set it using the setFullYear() function.
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var dateObj = new Date('23, August'); document.write("<br>"); dateObj.setFullYear(2018) document.write(dateObj.toDateString()); </script> </body> </html>
Output
Thu Aug 23 2018
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 year using this function and the month and date value remain same as the current date.
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var dateObj = new Date(); document.write("<br>"); dateObj.setFullYear(2018) document.write(dateObj.toDateString()); </script> </body> </html>
Output
Thu Oct 18 2018