A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay below:

Date.prototype.getDay() - JavaScript | MDN

Date.prototype.getDay()

Baseline Widely available

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
構文 引数

なし。

返値

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

解説

getDay() の返値は 0 から始まります。これは、例えば、曜日の配列をインデックス付けする場合に有益です。

const valentines = new Date("1995-02-14");
const day = valentines.getDay();
const dayNames = ["Sunday", "Monday", "Tuesday" /* , … */];

console.log(dayNames[day]); // "Monday"

ただし、国際化のためには、代わりに options 引数付き Intl.DateTimeFormat を使用することをお勧めします。

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 日は月曜日であるためです。

const xmas95 = new Date("1995-12-25T23:15:30");
const weekday = xmas95.getDay();

console.log(weekday); // 1
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4