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)
yearValue
is an integer that denotes the year.monthValue
is an integer in the range 0
to 11
, where 0
represents January and 11
represents December.dateValue
is an integer in the range 1
to 31
which denotes the date of the current month. The monthValue
must also be specified if the dateValue
is used.Note: Only the
yearValue
parameter is required, the rest are optional.
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.
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