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 Instanzen des Intl.DateTimeFormat formatiert ein Datum entsprechend der Lokalisierung und den Formatierungsoptionen dieses Intl.DateTimeFormat Objekts.

Probieren Sie es aus

const options1 = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
const date1 = new Date(2012, 5);

const dateTimeFormat1 = new Intl.DateTimeFormat("sr-RS", options1);
console.log(dateTimeFormat1.format(date1));
// Expected output: "петак, 1. јун 2012."

const dateTimeFormat2 = new Intl.DateTimeFormat("en-GB", options1);
console.log(dateTimeFormat2.format(date1));
// Expected output: "Friday, 1 June 2012"

const dateTimeFormat3 = new Intl.DateTimeFormat("en-US", options1);
console.log(dateTimeFormat3.format(date1));
// Expected output: "Friday, June 1, 2012"

Syntax

js
format(date)

Parameter

date

Das zu formatierende Datum. Kann ein Date- oder Temporal.PlainDateTime-Objekt sein. Zusätzlich kann es sich um ein Temporal.PlainTime, Temporal.PlainDate, Temporal.PlainYearMonth oder Temporal.PlainMonthDay-Objekt handeln, wenn das DateTimeFormat-Objekt so konfiguriert wurde, dass mindestens ein relevanter Teil des Datums ausgegeben wird.

Hinweis: Ein Temporal.ZonedDateTime-Objekt löst immer einen TypeError aus; verwenden Sie stattdessen Temporal.ZonedDateTime.prototype.toLocaleString() oder konvertieren Sie es in ein Temporal.PlainDateTime-Objekt.

Wenn der Parameter weggelassen wird, wird das aktuelle Datum (wie von Date.now() zurückgegeben) formatiert. Dies könnte leicht verwirrend sein, daher wird empfohlen, immer explizit ein Datum zu übergeben.

Rückgabewert

Ein String, der das angegebene date entsprechend der Lokalisierung und den Formatierungsoptionen dieses Intl.DateTimeFormat Objekts darstellt.

Hinweis: Meistens ist das Format, das von format() zurückgegeben wird, konsistent. Allerdings kann die Ausgabe je nach Implementierung variieren, selbst innerhalb derselben Lokalisierung — solche Variationen sind beabsichtigt und durch die Spezifikation erlaubt. Es könnte auch nicht dem entsprechen, was Sie erwarten. Zum Beispiel könnte der String geschützte Leerzeichen enthalten oder von bidirektionalen Steuerzeichen umgeben sein. Sie sollten die Ergebnisse von format() nicht mit fest kodierten Konstanten vergleichen.

Beispiele

Verwendung von format

Verwenden Sie die format-Getter-Funktion, um ein einzelnes Datum zu formatieren, hier für Serbien:

js
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, um alle Daten in einem Array zu formatieren. Beachten Sie, dass die Funktion an den Intl.DateTimeFormat gebunden ist, aus dem sie gewonnen wurde, sodass sie direkt an Array.prototype.map() übergeben werden kann.

js
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® 2025 Internationalization API Specification
# sec-intl.datetimeformat.prototype.format

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
format

Legend

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

Full support
Full support
See implementation notes.

Siehe auch