Home »
JavaScript
Date setFullYear() method with example in JavaScript
JavaScript Date setFullYear() method: Here, we are going to learn about the setFullYear() method in JavaScript with example.
Submitted by IncludeHelp, on March 13, 2019
JavaScript Date setFullYear() method
setFullYear() method is a Date class method, it is used to set the year (in 4 digits) to the Date object with a valid year value (between 1000 to 9999).
Syntax
var dt = new Date();
dt.setFullYear(year);
Sample Input/Output
Input/Date class object declaration:
var dt = new Date();
Function call to set the year:
dt.setFullYear(2022);
Function call to get the year:
dt.getFullYear();
Output:
2022
JavaScript code to demonstrate an example of Date.setFullYear() method
<html>
<head><title>JavaScipt Example</title></head>
<body>
<script>
var dt = new Date();
//getting current year
var year = dt.getFullYear();
//printing
document.write("year: " + year + "<br>");
//setting year to 2022
dt.setFullYear(2022);
//getting & printing year again
year = dt.getFullYear();
document.write("year: " + year + "<br>");
</script>
</body>
</html>
Output
year: 2019
year: 2022
Reference: JavaScript setFullYear() Method
JavaScript Date Object Methods »