The setFullYear() method sets the year using four digits for dates between year 1000 and 9999 for the date object.
The setFullYear() method sets the year using four digits for dates between year 1000 and 9999 for the date object.
You can use this method to set the month and day of month.
Date.setFullYear(year,month,day)
Parameter | Require | Description |
---|---|---|
year | Required. | A value representing the year, negative values are allowed |
month | Optional. | An integer representing the month |
day | Optional. | An integer representing the day of month |
For the month value, the expected values are 0-11, but other values are allowed:
For the day value, the expected values are 1-31, but other values are allowed:
A Number, representing the number of milliseconds between the date object and midnight January 1 1970
Set the year to 2020:
//display a date after changing the year. var d = new Date(); d.setFullYear(2020);/*from w w w . j av a 2s. c om*/ console.log(d); //Set the date to November 3, 2020: //display a date after changing the year, month, and day. var d = new Date(); d.setFullYear(2020, 10, 3); console.log(d); //JavaScript counts months from 0 to 11. Month 10 is November. //Set the date to six months ago: var d = new Date(); d.setFullYear(d.getFullYear(), d.getMonth() - 6); console.log(d);