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.

Die Methode getDay() von Date-Instanzen gibt den Wochentag für dieses Datum gemäß der lokalen Zeit zurück, wobei 0 für Sonntag steht. Für den Tag des Monats siehe Date.prototype.getDate().

Probieren Sie es aus

const birthday = new Date("August 19, 1975 23:15:30");
const day1 = birthday.getDay();
// Sunday - Saturday : 0 - 6

console.log(day1);
// Expected output: 2

Syntax

js
getDay()

Parameter

Keine.

Rückgabewert

Eine ganze Zahl zwischen 0 und 6, die den Wochentag für das angegebene Datum gemäß der lokalen Zeit darstellt: 0 für Sonntag, 1 für Montag, 2 für Dienstag, und so weiter. Gibt NaN zurück, wenn das Datum ungültig ist.

Beschreibung

Der Rückgabewert von getDay() ist nullbasiert, was nützlich ist, um beispielsweise auf Arrays von Wochentagen zuzugreifen:

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

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

Für Internationalisierungszwecke sollten Sie jedoch bevorzugt Intl.DateTimeFormat mit dem Parameter options verwenden.

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"

Beispiele

Verwendung von getDay()

Die Variable weekday hat den Wert 1, basierend auf dem Wert des Date-Objekts xmas95, da der 25. Dezember 1995 ein Montag ist.

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

console.log(weekday); // 1

Spezifikationen

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

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
getDay

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch