Intl.RelativeTimeFormat.prototype.formatToParts()

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.

Intl.RelativeTimeFormat.prototype.formatToParts() メソッドは、ロケールを考慮したカスタム書式設定に使用できる相対時間書式を部品単位で表すオブジェクトの配列 (Array) を返します。

試してみましょう

const rtf1 = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
const parts = rtf1.formatToParts(10, "seconds");

console.log(parts[0].value);
// Expected output: "in "

console.log(parts[1].value);
// Expected output: "10"

console.log(parts[2].value);
// Expected output: " seconds"

構文

js
RelativeTimeFormat.formatToParts(value, unit);

引数

value

国際化された相対時間のメッセージに使用する数値です。

unit

国際化された相対時間のメッセージに使用する単位です。利用可能な値は、 "year", "quarter", "month", "week", "day", "hour", "minute", "second" です。複数形も許容されています。

返値

書式化された相対時間を部品単位で含むオブジェクトの配列 (Array) です。

解説

Intl.RelativeTimeFormat.prototype.formatToParts メソッドは、書式化メソッドのバージョンの一つで、書式化された数値を他の周囲のテキストから分離し、それぞれの構成部品に分解した、オブジェクトの「部分」を表すオブジェクトの配列を返すものです。これらのオブジェクトには二つのプロパティがあります。 type は NumberFormat の formatToParts 型で、値は出力の構成要素である文字列です。もし "part" が NumberFormat から来たものであれば、書式化された単位を示す unit プロパティを持ちます。

formatToParts の使用

js
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" }]

仕様書

Specification
ECMAScript® 2025 Internationalization API Specification
# sec-Intl.RelativeTimeFormat.prototype.formatToParts

ブラウザーの互換性

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
formatToParts

Legend

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

Full support
Full support
See implementation notes.

関連情報