Date.prototype.setMonth()
Date
实例的 setMonth()
方法根据当地时间更改该日期的月份和/或日期。
¥The setMonth()
method of Date
instances changes the month and/or day of the month for this date according to local time.
Try it
语法
参数
返回值
¥Return value
就地更改 Date
对象,并返回其新的 timestamp。如果参数为 NaN
(或获取 coerced 到 NaN
的其他值,例如 undefined
),则日期设置为 失效日期,并返回 NaN
。
¥Changes the Date
object in place, and returns its new timestamp. If a parameter 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
参数,则使用与 getDate()
返回的值相同的值。
¥If you do not specify the dateValue
parameter, the same value as what is returned by getDate()
is used.
如果你指定的参数超出预期范围,则 Date
对象中的其他参数和日期信息将相应更新。例如,如果为 monthValue
指定 15,则年份加 1,月份使用 3。
¥If a parameter you specify is outside of the expected range, other parameters and the date information in the Date
object are updated accordingly. For example, if you specify 15 for monthValue
, the year is incremented by 1, and 3 is used for month.
当月的当前日期将影响此方法的行为。从概念上讲,它将把当前日期给定的天数与指定为参数的新月份的第一天相加,以返回新日期。例如,如果当前值为 2016 年 1 月 31 日,则调用值为 1 的 setMonth 将返回 2016 年 3 月 2 日。这是因为 2016 年 2 月有 29 天。
¥The current day of month will have an impact on the behavior of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st January 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
示例
使用 setMonth()
规范
Specification |
---|
ECMAScript Language Specification # sec-date.prototype.setmonth |
浏览器兼容性
BCD tables only load in the browser