Javascript - Date setFullYear() Method

The setFullYear() method sets the year using four digits for dates between year 1000 and 9999 for the date object.

Description

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.

Syntax

Date.setFullYear(year,month,day)

Parameter Values

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:

  • -1 will result in the last month of the previous year
  • 12 will result in the first month of the next year
  • 13 will result in the second month of the next year

For the day value, the expected values are 1-31, but other values are allowed:

  • 0 will result in the last day of the previous month
  • -1 will result in the day before the last day of the previous month
  • If the month has 31 days: 32 will result in the first day of the next month
  • If the month has 30 days: 32 will result in the second day of the next month

Return

A Number, representing the number of milliseconds between the date object and midnight January 1 1970

Example

Set the year to 2020:

Demo

//display a date after changing the year.
var d = new Date();
d.setFullYear(2020);/*from ww  w .  j  av  a 2s .  com*/
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);

Result