Date.prototype.getMonth()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
getMinutes() は Date インスタンスのメソッドで、この日付の「月」を表す 0 を基点とした値(すなわち 0 が年の最初の月を示す)を返します。
試してみましょう
const moonLanding = new Date("July 20, 69 00:20:18"); console.log(moonLanding.getMonth()); // (January gives 0) // 予想される結果: 6 構文
js
getMonth() 引数
なし。
返値
地方時に基づき、指定された日時の「月」を表す 0 から 11 までの間の整数値です。 1 月は 0、2 月 は 1 というようになります。日時が無効な場合は NaN を返します。
解説
getMonth() の返値は 0 から始まるため、例えば、月の配列のインデックス指定に有益です。
js
const valentines = new Date("1995-02-14"); const month = valentines.getMonth(); const monthNames = ["January", "February", "March" /* , … */]; console.log(monthNames[month]); // "February" ただし、国際化のためには、代わりに Intl.DateTimeFormat を options 引数付きで使用することをお勧めします。
js
const options = { month: "long" }; console.log(new Intl.DateTimeFormat("en-US", options).format(valentines)); // "February" console.log(new Intl.DateTimeFormat("de-DE", options).format(valentines)); // "Februar" 例
>getMonth() の使用
変数 month には、 Date オブジェクト xmas95 に基づいて、 11 という値が入ります。
js
const xmas95 = new Date("1995-12-25T23:15:30"); const month = xmas95.getMonth(); console.log(month); // 11 仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-date.prototype.getmonth> |