JavaScript setFullYear()Method

Date  对象参考手册JavaScript Date Object

Example

Set the year to 2020:

var d = new Date();
d.setFullYear(2020);

dOutput:


Try it Yourself »

Definition and Usage

The setFullYear() method is used to set the year.

This method can be used to set the month and the day of the month.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setFullYear() method.


Syntax

Date.setFullYear(year,month,day)

Parameter Values

Parameter Description
year Required. A four-digit integer representing the year. Expressed in local time.
month Optional. A number representing the month.

Expressed in local time. An integer value between 0 and 11, representing January to December.

  • -1 is the last month of the previous year
  • 12 is the first month of the next year
  • 13 is the second month of the next year
day Optional. A number representing the day of the month.

Expressed in local time. An integer between 1 and 31:

  • 0 is the last day of the previous month
  • -1 is the day before the last day of the previous month

If the month has 31 days:

  • 32 is the first day of the next month

If the month has 30 days:

  • 32 is the second day of the next month

Return Value

Type Description
Number The number of milliseconds between midnight January 1, 1970 and the adjusted date.

Technical Details

JavaScript Version: 1.3


More Examples

Example

In this example, we will set the date to November 3, 2020 with setFullYear():

var d=new Date();
d.setFullYear(2020,10,3);

dOutput:


Try it Yourself »

Example

Set the date to 6 months ago:

var d=new Date();
d.setFullYear(d.getFullYear,d.getMonth()-6);

dOutput:


Try it Yourself »


Date  对象参考手册JavaScript Date Object

Other Extensions