Temporal.PlainDate.prototype.daysInMonth

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimentell: Dies ist eine experimentelle Technologie
Überprüfen Sie die Browser-Kompatibilitätstabelle sorgfältig vor der Verwendung auf produktiven Webseiten.

Die daysInMonth Zugriffs-Eigenschaft von Temporal.PlainDate-Instanzen gibt eine positive ganze Zahl zurück, die die Anzahl der Tage im Monat dieses Datums darstellt. Sie ist kalenderabhängig (calendar).

Beachten Sie, dass die Tage im Monat nicht immer der day des letzten Tages des Monats entsprechen, in dem seltenen Fall, dass in einem Monat einige Tage übersprungen werden.

Der Set-Zugriff von daysInMonth ist undefined. Sie können diese Eigenschaft nicht direkt ändern.

Beispiele

Verwendung von daysInMonth

js
const date = Temporal.PlainDate.from("2021-07-01");
console.log(date.daysInMonth); // 31

const date2 = Temporal.PlainDate.from("2021-02-01");
console.log(date2.daysInMonth); // 28; 2021 is not a leap year

const date3 = Temporal.PlainDate.from("2020-02-01");
console.log(date3.daysInMonth); // 29; 2020 is a leap year

const date4 = Temporal.PlainDate.from("2021-04-01[u-ca=chinese]");
console.log(date4.month); // 2
console.log(date4.daysInMonth); // 30; the Chinese 2nd month has 30 days

Wechsel zum vorletzten Tag des Monats

Sie können daysInMonth verwenden, um zum vorletzten Tag des Monats zu wechseln:

js
const date = Temporal.PlainDate.from("2021-07-01");
const secondLastDay = date.with({ day: date.daysInMonth - 1 });
console.log(secondLastDay.toString()); // 2021-07-30

Dies ist jedoch nicht völlig sicher, da daysInMonth nicht garantiert ist, dass es eine Verbindung mit dem Tagesindex hat. Hier ist eine sicherere Möglichkeit, den vorletzten Tag zu erhalten:

js
const date = Temporal.PlainDate.from("2021-07-01");
const secondLastDay = date
  .with({ day: Number.MAX_SAFE_INTEGER })
  .subtract({ days: 1 });
console.log(secondLastDay.toString()); // 2021-07-30

Spezifikationen

Specification
Temporal proposal
# sec-get-temporal.plaindate.prototype.daysinmonth

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
daysInMonth
Experimental

Legend

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

No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.

Siehe auch