Intl.RelativeTimeFormat
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Das Intl.RelativeTimeFormat
Objekt ermöglicht sprachsensitives Formatieren relativer Zeitangaben.
Probieren Sie es aus
const rtf1 = new Intl.RelativeTimeFormat("en", { style: "short" });
console.log(rtf1.format(3, "quarter"));
// Expected output: "in 3 qtrs."
console.log(rtf1.format(-1, "day"));
// Expected output: "1 day ago"
const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
console.log(rtf2.format(2, "day"));
// Expected output: "pasado mañana"
Konstruktor
Intl.RelativeTimeFormat()
-
Erstellt ein neues
Intl.RelativeTimeFormat
Objekt.
Statische Methoden
Intl.RelativeTimeFormat.supportedLocalesOf()
-
Gibt ein Array zurück, das die bereitgestellten Locales enthält, die unterstützt werden, ohne auf die Standard-Locale zur Laufzeit zurückgreifen zu müssen.
Instanzeigenschaften
Diese Eigenschaften sind auf Intl.RelativeTimeFormat.prototype
definiert und werden von allen Instanzen von Intl.RelativeTimeFormat
geteilt.
Intl.RelativeTimeFormat.prototype.constructor
-
Die Konstruktorfunktion, die das Instanzobjekt erstellt hat. Für
Intl.RelativeTimeFormat
Instanzen ist der Anfangswert derIntl.RelativeTimeFormat
Konstruktor. Intl.RelativeTimeFormat.prototype[Symbol.toStringTag]
-
Der Anfangswert der
[Symbol.toStringTag]
Eigenschaft ist der String"Intl.RelativeTimeFormat"
. Diese Eigenschaft wird inObject.prototype.toString()
verwendet.
Instanzmethoden
Intl.RelativeTimeFormat.prototype.format()
-
Formatiert einen
value
und eineunit
entsprechend der Locale und den Formatierungsoptionen des gegebenenIntl.RelativeTimeFormat
Objekts. Intl.RelativeTimeFormat.prototype.formatToParts()
-
Gibt ein
Array
von Objekten zurück, die das relative Zeitformat in Teilen repräsentieren, welche für eine benutzerdefinierte, localesensitive Formatierung verwendet werden können. Intl.RelativeTimeFormat.prototype.resolvedOptions()
-
Gibt ein neues Objekt mit Eigenschaften zurück, die die Locale und die während der Initialisierung des Objekts berechneten Formatierungsoptionen widerspiegeln.
Beispiele
Grundlegende Verwendung des Formats
Das folgende Beispiel zeigt, wie ein relativer Zeitformatierer für die englische Sprache verwendet wird.
// Create a relative time formatter in your locale
// with default values explicitly passed in.
const rtf = new Intl.RelativeTimeFormat("en", {
localeMatcher: "best fit", // other values: "lookup"
numeric: "always", // other values: "auto"
style: "long", // other values: "short" or "narrow"
});
// Format relative time using negative value (-1).
rtf.format(-1, "day"); // "1 day ago"
// Format relative time using positive value (1).
rtf.format(1, "day"); // "in 1 day"
Verwendung von formatToParts
Das folgende Beispiel zeigt, wie ein relativer Zeitformatierer erstellt wird, der formatierte Teile zurückgibt.
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
// Format relative time using the day unit.
rtf.formatToParts(-1, "day");
// [{ type: "literal", value: "yesterday"}]
rtf.formatToParts(100, "day");
// [
// { type: "literal", value: "in " },
// { type: "integer", value: "100", unit: "day" },
// { type: "literal", value: " days" }
// ]
Spezifikationen
Specification |
---|
ECMAScript® 2025 Internationalization API Specification # relativetimeformat-objects |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RelativeTimeFormat | ||||||||||||||
RelativeTimeFormat() constructor | ||||||||||||||
locales parameter | ||||||||||||||
format | ||||||||||||||
formatToParts | ||||||||||||||
resolvedOptions | ||||||||||||||
supportedLocalesOf |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- See implementation notes.
- Has more compatibility info.
Siehe auch
- Polyfill von
Intl.RelativeTimeFormat
in FormatJS Intl
Intl.RelativeTimeFormat
auf v8.dev (2018)