Intl.DateTimeFormat.prototype.format()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
Die format() Methode von Intl.DateTimeFormat Instanzen formatiert ein Datum gemäß den Lokalisierungs- und Formatierungsoptionen dieses Intl.DateTimeFormat Objekts.
Probieren Sie es aus
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const date = new Date(2012, 5);
const dateTimeFormat1 = new Intl.DateTimeFormat("sr-RS", options);
console.log(dateTimeFormat1.format(date));
// Expected output: "петак, 1. јун 2012."
const dateTimeFormat2 = new Intl.DateTimeFormat("en-GB", options);
console.log(dateTimeFormat2.format(date));
// Expected output: "Friday, 1 June 2012"
const dateTimeFormat3 = new Intl.DateTimeFormat("en-US", options);
console.log(dateTimeFormat3.format(date));
// Expected output: "Friday, June 1, 2012"
Syntax
format(date)
Parameter
date-
Das zu formatierende Datum. Kann ein
DateoderTemporal.PlainDateTimeObjekt sein. Zusätzlich kann es einTemporal.PlainTime,Temporal.PlainDate,Temporal.PlainYearMonthoderTemporal.PlainMonthDayObjekt sein, wenn dasDateTimeFormatObjekt konfiguriert wurde, um mindestens einen relevanten Teil des Datums zu drucken.Hinweis: Ein
Temporal.ZonedDateTimeObjekt wird immer einenTypeErrorauslösen; verwenden Sie stattdessenTemporal.ZonedDateTime.prototype.toLocaleString()oder konvertieren Sie es in einTemporal.PlainDateTimeObjekt.Wenn es weggelassen wird, wird das aktuelle Datum formatiert (wie von
Date.now()zurückgegeben), was leicht verwirrend sein kann. Daher ist es ratsam, immer explizit ein Datum zu übergeben.
Rückgabewert
Ein String, der das angegebene date gemäß den Lokalisierungs- und Formatierungsoptionen dieses Intl.DateTimeFormat Objekts darstellt.
Hinweis:
Meistens ist das von format() zurückgegebene Format konsistent. Allerdings kann die Ausgabe zwischen Implementierungen variieren, selbst innerhalb derselben Lokalisierung - Ausgabevariationen sind absichtlich und durch die Spezifikation erlaubt. Es mag auch nicht dem entsprechen, was Sie erwarten. Zum Beispiel könnte der String nicht trennende Leerzeichen verwenden oder von bidirektionalen Steuerzeichen umgeben sein. Sie sollten die Ergebnisse von format() nicht mit festgelegten Konstanten vergleichen.
Beispiele
>Verwendung von format
Verwenden Sie die format Getter-Funktion zum Formatieren eines einzelnen Datums, hier für Serbien:
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const dateTimeFormat = new Intl.DateTimeFormat("sr-RS", options);
console.log(dateTimeFormat.format(new Date()));
// "недеља, 7. април 2013."
Verwendung von format mit map
Verwenden Sie die format Getter-Funktion zum Formatieren aller Daten in einem Array. Beachten Sie, dass die Funktion an das Intl.DateTimeFormat, aus dem sie stammt, gebunden ist, sodass sie direkt an Array.prototype.map() übergeben werden kann.
const a = [new Date(2012, 8), new Date(2012, 11), new Date(2012, 3)];
const options = { year: "numeric", month: "long" };
const dateTimeFormat = new Intl.DateTimeFormat("pt-BR", options);
const formatted = a.map(dateTimeFormat.format);
console.log(formatted.join("; "));
// "setembro de 2012; dezembro de 2012; abril de 2012"
Spezifikationen
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification> # sec-intl.datetimeformat.prototype.format> |
Browser-Kompatibilität
Loading…