Date.prototype.getDay()

getDay() メソッドは、地方時に基づき、指定された日付の曜日を返します。 0 は日曜日を表します。「日」を取得する方法は Date.prototype.getDate() をご覧ください。

試してみましょう

構文

js
getDay()

返値

整数値で、 0 から 6 までの値を取り、地方時に基づいて指定された日付の曜日に対応し、 0 は日曜日、 1 は月曜日、 2 は火曜日のようになります。

getDay の使用

以下の 2 行目の文は、 Date オブジェクトである xmas95 の値に基づき、weekday に 1 という値を代入します。1995 年 12 月 25 日は月曜日です。

js
const xmas95 = new Date("December 25, 1995 23:15:30");
const weekday = xmas95.getDay();

console.log(weekday); // 1

メモ: 必要であれば、曜日の完全な名前 (例えば "Monday") は Intl.DateTimeFormatoptions 引数を設定することで取得することができます。このメソッドを使用すれば、国際化がより簡単になります。

js
const options = { weekday: "long" };
console.log(new Intl.DateTimeFormat("en-US", options).format(Xmas95));
// Monday
console.log(new Intl.DateTimeFormat("de-DE", options).format(Xmas95));
// Montag

仕様書

Specification
ECMAScript Language Specification
# sec-date.prototype.getday

ブラウザーの互換性

BCD tables only load in the browser

関連情報