目录

JavaScript Date setUTCFullYear()

示例

将年份设置为 2015 年:

const d = new Date();
d.setUTCFullYear(2015);
亲自试一试 »

下面有更多 "亲自试一试" 示例。


描述

setUTCFullYear() 方法根据 UTC 设置日期对象的年份。

笔记

UTC(通用协调时间)是世界时间标准设定的时间。

UTC 时间与 GMT 时间(格林威治标准时间)相同。


浏览器支持

setUTCFullYear()是 ECMAScript1 (ES1) 功能。

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

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

语法

Date.setUTCFullYear( year, month, day)

参数值

Parameter Description
year Required. A value representing the year, negative values are allowed
month Optional. An integer representing the month

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
day Optional. An integer representing the day of month

Expected values are 1-31, but other values are allowed:

  • 0 will result in the last hour of the previous month
  • -1 will result in the hour before the last hour 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

返回值

Type Description
Number Milliseconds between the date object and midnight January 1 1970


技术细节

JavaScript 版本: ECMA脚本1

更多示例

示例2

将日期设置为 2020 年 11 月 3 日(UTC 时间):

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

示例

将日期设置为六个月, UTC 时间:

const d = new Date();
d.setUTCFullYear(d.getUTCFullYear, d.getUTCMonth() - 6);
亲自试一试 »