Date.prototype.getMonth()
getMonth()
メソッドは、地方時に基づき、指定された日付の「月」を表す 0 を基点とした値 (すなわち 0 が年の最初の月を示す) を返します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
dateObj.getMonth()
返値
地方時に基づき、与えた日付の「月」を表す 0 から 11 までの間の整数値。0 は 1 月、1 は 2 月、11 は 12 月に対応します。
例
getMonth() の使用
以下の 2 行目の文は、Date
オブジェクト Xmas95
の値に基づき、変数 month
に 11 という値を代入します。
var Xmas95 = new Date('December 25, 1995 23:15:30');
var month = Xmas95.getMonth();
console.log(month); // 11
注: 必要であれば、月の完全な名前 (例えば January
) は Intl.DateTimeFormat()
に options
引数を付けることで取得することができます。このメソッドを使用すれば、国際化がより簡単になります。
var options = { month: 'long'};
console.log(new Intl.DateTimeFormat('en-US', options).format(Xmas95));
// December
console.log(new Intl.DateTimeFormat('de-DE', options).format(Xmas95));
// Dezember
仕様書
ブラウザーの互換性
BCD tables only load in the browser