Latest web development tutorials

JavaScript setFullYear () method

Date Object Reference JavaScript Date Object

Examples

Year is set to 2020:

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

d output:


try it"

Definition and Usage

setFullYear () method is used to set the year.

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support setFullYear () method


grammar

Date.setFullYear( year , month , day )

Parameter Value

parameter description
year Required. It represents an integer of four years. Expressed in local time.
month Optional. It represents a number of the month.

Expressed in local time. Between 0 and 11:

  • -1 Is the last month of last year
  • 12 for the first month of next year
  • 13 for the first month of next year
day Optional. Day of the month value.

Expressed in local time. Between 1 to 31:

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

If the month has 31 days:

  • 32 is the first day of the next month

If the month has 30 days:

  • 32 for the second day of the following month

return value

类型 描述
Number 1970年1月1日午夜至调整过日期的毫秒。

technical details

JavaScript version: 1.3


More examples

Examples

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

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

d output:


try it"

Examples

Set the date six months ago:

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

d output:


try it"


Date Object Reference JavaScript Date Object