JavaScript

Intl.RelativeTimeFormat.prototype.formatToParts() メソッド


編集:2023.01.12 by T.A.

[Edge]。→

Intl.RelativeTimeFormat で、指定内容を相対時間として書式化した各部分の情報オブジェクトを配列で返します。

構文
Intl.RelativeTimeFormat.formatToParts(value, unit)
Intl.RelativeTimeFormat Intl.RelativeTimeFormat
value 相対値を表す数値。
unit 国際化された相対時間のメッセージに使用する単位を表す文字列。値: yearquartermonthweekdayhourminutesecond。→format():unit
戻り値

書式化された各部品のオブジェクトの配列。オブジェクトには以下のプロパティが含まれる。

type
¶タイプを表す。値は文字列。

値例: integer、literal。

value
¶値を表す。値は文字列。例「」内のフォントは「MS ゴシック」。

値例: 「1」、「」、「 週間前」。

unit
¶単位。値は文字列。これは含まれないこともある。

値例: week。

DA=new Intl.RelativeTimeFormat('ja');  //日本語。
D=DA.formatToParts(-1, 'week');  //-1で週。
	//=[object]:[object Object],[object Object]
	//0=[object]:[object Object]
	//1=[object]:[object Object]
D[0]=[object]:[object Object]
	//type=[string]:integer
	//value=[string]:1
	//unit=[string]:week
D[1]=[object]:[object Object]
	//type=[string]:literal
	//value=[string]: 週間前
DA=new Intl.RelativeTimeFormat('ja-u-nu-hanidec');  //日本語、数字を漢字。
D=DA.formatToParts(-1, 'week');  //-1で週。
	//=[object]:[object Object],[object Object]
	//0=[object]:[object Object]
	//1=[object]:[object Object]
D[0]=[object]:[object Object]
	//type=[string]:integer
	//value=[string]:一
	//unit=[string]:week
D[1]=[object]:[object Object]
	//type=[string]:literal
	//value=[string]: 週間前

RelativeTimeFormat()format()


テスト