Die Intl.DateTimeFormat.prototype.format
Eigenschaft gibt einen Getter-Funktion zurück, die einen Zeitstempel nach den Gebiets- und Formatierungsoptionen des Intl.DateTimeFormat
Objekts formatiert.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
dateTimeFormat.format(date)
Parameter
date
- Der Zeitstempel, der formatiert werden soll.
Beschreibung
Die Funktion, die vom format
Getter zurückgegeben wird, formatiert einen Zeitpunkt (date) in einen String. Dabei werden die Gebiets- und Formatierungsoptionen des Intl.DateTimeFormat
Objekts berücksichtigt.
Beispiele
Einsatz von format
Die vom format
Getter zurückgegebene Funktion wird zum Formatieren von Zeitstempeln genutzt, hier für Serbien:
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var dateTimeFormat = new Intl.DateTimeFormat('sr-RS', options);
console.log(dateTimeFormat.format(new Date()));
// → "недеља, 7. април 2013."
Einsatz von format
mit map
Die vom format
Getter zurückgegebene Funktion kann zum Formatieren von Zeitpunkten in einem Array genutzt werden. Zu berücksichtigen ist, dass die Funktion an das Intl.DateTimeFormat
Objekt gebunden ist, von welche die Funktion stammt, so dass sie direkt an Array.prototype.map()
übergeben werden kann.
var a = [new Date(2012, 08), new Date(2012, 11), new Date(2012, 03)];
var options = { year: 'numeric', month: 'long' };
var dateTimeFormat = new Intl.DateTimeFormat('pt-BR', options);
var formatted = a.map(dateTimeFormat.format);
console.log(formatted.join('; '));
// → "setembro de 2012; dezembro de 2012; abril de 2012"
Spezifikationen
Browserkompatibilität
BCD tables only load in the browser