Date.prototype.setMonth()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
setMonth()
は Date
インスタンスのメソッドで、現在設定されている年に基づき、指定された日時の「月」を設定します。
試してみましょう
const event = new Date("August 19, 1975 23:15:30");
event.setMonth(3);
console.log(event.getMonth());
// 予想される結果: 3
console.log(event);
// 予想される結果: "Sat Apr 19 1975 23:15:30 GMT+0100 (CET)"
// Note: your timezone may vary
構文
js
setMonth(monthValue)
setMonth(monthValue, dateValue)
引数
monthValue
-
月を表す整数で、 0 は 1 月、 1 を 2 月、というようになります。
dateValue
省略可-
「日」を表す 1 から 31 までの間の整数値。
返値
解説
dateValue
引数を指定しなかった場合、getDate()
メソッドから返される値が使われます。
指定した値が期待される日付の範囲外の場合、それに応じて setMonth()
が Date
オブジェクトの日付情報の更新を試みます。例えば、monthValue
に 15 を指定した場合、年に 1 が加算され、月が 3 になります。
このメソッドの動作は、ある特定の月の日にちに影響を与えます。 概念上は、その月の日数を、引数として与えた新しい月の初日に加え、新しい日付を返します。 例えば、現在の値が 2016 年 1 月 31 日である場合、1 を引数として setMonth を呼び出すと 2016 年 3 月 2 日が返ってきます。 これは、2016 年の 2 月が 29 日間しかないからです。
例
setMonth() の使用
js
const theBigDay = new Date();
theBigDay.setMonth(6);
// 月末の移行にご注意ください
const endOfMonth = new Date(2016, 7, 31);
endOfMonth.setMonth(1);
console.log(endOfMonth); // Wed Mar 02 2016 00:00:00
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-date.prototype.setmonth |