--- title: Intl.RelativeTimeFormat slug: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat tags: - Class - Internationalization - Intl - JavaScript - RelativeTimeFormat translation_of: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat ---
Intl.RelativeTimeFormat
オブジェクトは言語に依存の相対時間の書式化を可能にします。
Intl.RelativeTimeFormat
オブジェクトを生成します。value
および unit
を、指定された {{jsxref("Intl.RelativeTimeFormat")}} オブジェクトのロケールと書式化オプションに従って書式化します。format
の使用例以下は英語の相対時間フォーマッターの使い方の例です。
// 明示的に渡された既定値を使って // ロケールの相対時間を生成します const rtf = new Intl.RelativeTimeFormat("en", { localeMatcher: "best fit", // other values: "lookup" numeric: "always", // other values: "auto" style: "long", // other values: "short" or "narrow" }); // 負数の値 (-1) を使った相対時間のフォーマット rtf.format(-1, "day"); // > "1 day ago" // 正数の値 (1) を使った相対時間のフォーマット rtf.format(1, "day"); // > "in 1 day"
以下はフォーマットされた部品を返す相対時間フォーマッターの生成方法の例です。
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" }); // 日単位の相対時間フォーマット 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" }]
仕様書 | 状態 | 備考 |
---|---|---|
{{SpecName('ES Int Draft', '#relativetimeformat-objects', 'RelativeTimeFormat')}} | 第 4 段階 |
{{Compat("javascript.builtins.Intl.RelativeTimeFormat")}}