A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.cdn.mozilla.net/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth below:

Date.prototype.setMonth() - JavaScript | MDN

Date.prototype.setMonth()

Baseline Widely available

setMonth() 方法根据本地时间为一个日期对象设置月份。

尝试一下
const event = new Date("August 19, 1975 23:15:30");

event.setMonth(3);

console.log(event.getMonth());
// Expected output: 3

console.log(event);
// Expected output: "Sat Apr 19 1975 23:15:30 GMT+0100 (CET)"
// Note: your timezone may vary
语法
dateObj.setMonth(monthValue[, dayValue])
JavaScript 1.3 版本之前
dateObj.setMonth(monthValue)
参数
monthValue

介于 0 到 11 之间的整数(表示一月到十二月)。

dayValue

从 1 到 31 之间的整数,表示月份中的第几天。0 为上个月最后一天

返回值!

基于 1 January 1970 00:00:00 UTC 开始计算的毫秒数

描述

如果不指定 dayValue 参数,就会使用 getDate 方法的返回值。

如果有一个指定的参数超出了合理范围,setMonth 会相应地更新日期对象中的日期信息。例如,为 monthValue 指定 15,则年份会加 1,月份将会使用 3。

示例 示例:使用 setMonth 方法
var theBigDay = new Date();
theBigDay.setMonth(6);
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4