Date.prototype.setUTCDate()

Date 实例的 setUTCDate() 方法根据世界时间更改该日期的月份日期。

¥The setUTCDate() method of Date instances changes the day of the month for this date according to universal time.

Try it

语法

¥Syntax

js
setUTCDate(dateValue)

参数

¥Parameters

dateValue

1 到 31 之间的整数,表示月份中的第几天。

返回值

¥Return value

就地更改 Date 对象,并返回其新的 timestamp。如果 dateValueNaN(或获取 coercedNaN 的其他值,例如 undefined),则日期设置为 失效日期 并返回 NaN

¥Changes the Date object in place, and returns its new timestamp. If dateValue is NaN (or other values that get coerced to NaN, such as undefined), the date is set to Invalid Date and NaN is returned.

描述

¥Description

如果 dateValue 超出该月的日期值范围,setDate() 将相应更新 Date 对象。

¥If the dateValue is outside of the range of date values for the month, setDate() will update the Date object accordingly.

例如,如果为 dateValue 提供 0,则日期将设置为上个月的最后一天。如果对 dateValue 使用 40,并且 Date 对象中存储的月份是六月,则日期将更改为 10,月份将增加到七月。

¥For example, if 0 is provided for dateValue, the date will be set to the last day of the previous month. If you use 40 for dateValue, and the month stored in the Date object is June, the day will be changed to 10 and the month will be incremented to July.

如果为 dateValue 提供负数,则日期将从上个月的最后一天开始倒数设置。-1 将导致日期设置为上个月最后一天之前 1 天。

¥If a negative number is provided for dateValue, the date will be set counting backwards from the last day of the previous month. -1 would result in the date being set to 1 day before the last day of the previous month.

示例

¥Examples

使用 setUTCDate()

¥Using setUTCDate()

js
const theBigDay = new Date();
theBigDay.setUTCDate(20);

规范

Specification
ECMAScript Language Specification
# sec-date.prototype.setutcdate

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看