Date.prototype.getDay()
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.
getDay()
は Date
インスタンスのメソッドで、地方時に基づき、この日付の曜日を返します。 0 は日曜日を表します。「日」を取得する方法は Date.prototype.getDate()
をご覧ください。
試してみましょう
const birthday = new Date("August 19, 1975 23:15:30");
const day1 = birthday.getDay();
// 日曜日 - 土曜日 : 0 - 6
console.log(day1);
// 予想される結果: 2
構文
js
getDay()
引数
なし。
返値
整数値で、 0 から 6 までの値を取り、地方時に基づいて指定された日時の曜日に対応し、 0 は日曜日、 1 は月曜日、 2 は火曜日のようになります。日時が無効な場合は NaN
を返します。
解説
getDay()
の返値は 0 から始まります。これは、例えば、曜日の配列をインデックス付けする場合に有益です。
js
const valentines = new Date("1995-02-14");
const day = valentines.getDay();
const dayNames = ["Sunday", "Monday", "Tuesday" /* , … */];
console.log(dayNames[day]); // "Monday"
ただし、国際化のためには、代わりに options
引数付き Intl.DateTimeFormat
を使用することをお勧めします。
js
const options = { weekday: "long" };
console.log(new Intl.DateTimeFormat("en-US", options).format(valentines));
// "Monday"
console.log(new Intl.DateTimeFormat("de-DE", options).format(valentines));
// "Montag"
例
getDay() の使用
変数 weekday
には、Date
オブジェクト xmas95
の値に基づいて、値 1
が指定されます。これは、1995 年 12 月 25 日は月曜日であるためです。
js
const xmas95 = new Date("1995-12-25T23:15:30");
const weekday = xmas95.getDay();
console.log(weekday); // 1
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-date.prototype.getday |