JavaScript setFullYear function is used to set the Year, Month, Day of a given date as per local time. The syntax of the setFullYear function is:
Date.setFullYear(Year, Month_Number, Day_Number)
In this Date Function both the Month Number and Day Number arguments are optional.
JavaScript setFullYear Function Example
Here, we are using setFullYear to set the current year to 2014.
<!DOCTYPE html> <html> <head> <title> JavaScript Set Full Year Functions </title> </head> <body> <h1> JavaScript setFullYear Function Example </h1> <script> var dt = Date(); document.write("Date and Time : " + dt + "<br/>"); dt.setFullYear(2014); document.write("After setFullYear() : " + dt); </script> </body> </html>
OUTPUT
JavaScript set Full Year Function Example 2
In this setFullYear example, we set the Full Year of a custom date to 2014, and Month to December.
<!DOCTYPE html> <html> <head> <title> JavaScript Set Full Year Functions </title> </head> <body> <h1> JavaScript setFullYear Function Example </h1> <script> var dt = Date("January 1, 2017 10:11:22"); document.write("Date and Time : " + dt + "<br/>"); dt.setFullYear(2014, 11); document.write("After setFullYear() : " + dt); </script> </body> </html>
OUTPUT
In this JavaScript set Full Year example, we set the Year to 2014, Month to December, and Day to 31
<!DOCTYPE html> <html> <head> <title> JavaScript Set Full Year Functions </title> </head> <body> <h1> JavaScript setFullYear Function Example </h1> <script> var dt = Date("January 1, 2017 10:11:22"); document.write("Date and Time : " + dt + "<br/>"); dt.setFullYear(2014, 11, 31); document.write("After setFullYear() : " + dt); </script> </body> </html>
OUTPUT