What is setFullYear() in JavaScript?
The setFullYear function in JavaScript is a method of the Date class with the following syntax:
setFullYear(yearValue)
setFullYear(yearValue, monthValue)
setFullYear(yearValue, monthValue, dateValue)
Parameters
yearValueis an integer that denotes the year.monthValueis an integer in the range0to11, where0represents January and11represents December.dateValueis an integer in the range1to31which denotes the date of the current month. ThemonthValuemust also be specified if thedateValueis used.
Note: Only the
yearValueparameter is required, the rest are optional.
Return value
setFullYear()returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC till the updated time.
The setFullYear function is called on a Date object, which results in the specified parameters being changed in the Date object.
Example
The following code demonstrates the usage of the setFullYear function:
let d1 = new Date('June 29, 2021 15:39:05:32');let d2 = new Date('June 29, 2021 15:39:05:32');let d3 = new Date('June 29, 2021 15:39:05:32');console.log('Dates Before Changing:');console.log('d1: ', d1);console.log('d2: ', d2);console.log('d3: ', d3);console.log('Return Values:');console.log(d1.setFullYear(2008));console.log(d2.setFullYear(2008, 8));console.log(d3.setFullYear(2008, 8, 8));console.log('Dates After Changing:');console.log('d1: ', d1);console.log('d2: ', d2);console.log('d3: ', d3);
In the example above, the variables d1, d2, and d3 are Date objects that initially have the same date, June 29, 2021 15:39:05:32. The setFullYear function is applied to all of the variables, with varying parameters. For d1, only the year is set to 2008. For d2, both year and month are set to 2008 and September respectively. And for d3, year, month, and date are set to 2008, September, and 8 respectively.
The example above also shows the return value from the function calls after Return Values: is printed onto the standard output.
Free Resources