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);

Examples:

    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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.