目录

JavaScript Date setFullYear()

示例1

const d = new Date();
d.setFullYear(2020);
亲自试一试 »

描述

setFullYear()设置日期的年份。

setFullYear()还可以设置月份和日期。

示例2

const d = new Date();
d.setFullYear(2020, 10, 3);
亲自试一试 »

示例3

将日期设置为六个月:

const d = new Date();
d.setFullYear(d.getFullYear(), d.getMonth() - 6);
亲自试一试 »

浏览器支持

setFullYear()是 ECMAScript1 (ES1) 功能。

所有浏览器均完全支持 ES1 (JavaScript 1997):

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


语法

Date.setFullYear( year, month, day)

参数

year Required.
The year. Negative values are allowed.
month Optional.
The month (0 to 11).

Higher and lower values are handeled with date maths:

  • -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
day Optional.
The day (0 to 31).

Higher and lower values are handeled with date maths:

  • 0 gives the last day of the previous month
  • -1 gives the day before the last day of the previous month
  • 32 gives the first day of the next month (if 31 days)
  • 41 gives the tenth day of the next month (if 31 days)

返回值

一个号码。

该日期与 1970 年 1 月 1 日 00:00:00 UTC 之间的毫秒数。