Intl.RelativeTimeFormat【国際化相対時間フォーマット】オブジェクト
メモ
概要
- 国際化対応の相対時間フォーマット オブジェクト
基本操作
- ロケール(省略可) を指定して コンストラクタ でオブジェクトを生成 (必要に応じてオプションで形式スタイル等も指定)
- format【フォーマット】で時間単位のフォーマット文字列を取得
関連
外部リンク
- ECMA-402 (英語)
RelativeTimeFormat Objects ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
コンストラクタ・メソッド
メソッド | 説明 |
---|---|
new Intl.RelativeTimeFormat( [ locales [ , options ] ] ) | コンストラクタ |
メソッド | 説明 | |
---|---|---|
Intl.RelativeTimeFormat.prototype. | format ( value, unit ) | フォーマット |
Intl.RelativeTimeFormat.prototype. | formatToParts ( value, unit ) | フォーマットパーツ取得 |
Intl.RelativeTimeFormat.prototype. | resolvedOptions ( ) | オプション取得 |
Intl.RelativeTimeFormat. | supportedLocalesOf ( locales [, options ] ) | サポート ロケール取得 |
プロパティ
プロパティ | 説明 | |
---|---|---|
Intl.RelativeTimeFormat.prototype | [ @@toStringTag ] | タグ (デフォルト:'Intl.RelativeTimeFormat') 実装:RelativeTimeFormat[ Symbol.toStringTag ] |
Intl.RelativeTimeFormat.prototype. | constructor | コンストラクタ定義 |
Intl.RelativeTimeFormat. | prototype | プロトタイプ |
const formatter = new Intl.RelativeTimeFormat("ja");
// [ @@toStringTag ]
console.log(formatter[Symbol.toStringTag]);
// 出力例:Intl.RelativeTimeFormat
console.log(formatter.constructor);
// 出力例:function RelativeTimeFormat()
console.log(Intl.RelativeTimeFormat.prototype);
// 出力例:
// Object { … }
// constructor: function RelativeTimeFormat()
// format: function format()
// formatToParts: function formatToParts()
// resolvedOptions: function resolvedOptions()
// Symbol(Symbol.toStringTag): "Intl.RelativeTimeFormat"
// <prototype>: Object { … }
new Intl.RelativeTimeFormat【コンストラクタ】
メモ
- Intl.RelativeTimeFormat【国際化相対時間フォーマット】 オブジェクトを生成
- 関連
- 外部リンク
- ECMA-402 (英語)
Intl.RelativeTimeFormat ( [ locales [ , options ] ] ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) - ISO 639-1コード一覧
- BCP 47 (Best Current Practice) [英語]
- ECMA-402 (英語)
構文
new Intl.RelativeTimeFormat([locales[, options]])
Intl.RelativeTimeFormat【国際化相対時間フォーマット】オブジェクト
localesロケール (BCP 47 の言語タグ等 + Unicode 拡張 数値フォーマット)〔実装依存〕
省略:デフォルトのロケール〔実装依存〕
文字列:1ロケール指定
文字列の配列:複数指定可 (適切なロケールを自動選択)
optionsオプション の組合せ
locales (ロケール) 実装依存
BCP 47 の言語タグ (一例)
値 | 備考 |
---|---|
ja | 日本語 |
ja-JP | 日本語 (日本) |
en-US | 英語 (アメリカ) |
en-GB | 英語 (イギリス) |
de-DE | ドイツ語 (ドイツ) |
fr-FR | フランス語 (フランス) |
ISO 639-1・639-2 (言語コード) 一例
ISO 639-1 | ISO 639-2 | ISO 639-3 | 備考 |
---|---|---|---|
ja | jpn | jpn | 日本語 |
en | eng | eng | 英語 |
de | deu ger | deu | ドイツ語 |
fr | fra fre | fra | フランス語 |
Unicode 拡張 数値フォーマット 実装依存
値 (-u-nu-○) | 説明 | 例 | コード |
---|---|---|---|
arab | アラビア・インド数字 | ٩ ~ ٠ | U+0660 ~ U+0669 |
arabext | ペルシア数字 | ۰ ~ ۹ | U+06F0 ~ U+06F9 |
bali | バリ数字 | ― | U+1B50 ~ U+1B59 |
beng | ベンガル数字 | ০ ~ ৯ | U+09E6 ~ U+09EF |
deva | デーヴァナーガリー数字 | ० ~ ९ | U+0966 ~ U+096F |
fullwide | 全角数字 | 0 ~ 9 | U+FF10 ~ U+FF19 |
gujr | グジャラート数字 | ૦ ~ ૯ | U+0AE6 ~ U+0AEF |
guru | グルムキー数字 | ੦ ~ ੯ | U+0A66 ~ U+0A6F |
hanidec | 漢数字 | 〇 ~ 九 | U+3007, U+4E00, U+4E8C, U+4E09, U+56DB, U+4E94, U+516D, U+4E03, U+516B, U+4E5D |
khmr | クメール数字 | ០ ~ ៩ | U+17E0 ~ U+17E9 |
knda | カンナダ数字 | ೦ ~ ೯ | U+0CE6 ~ U+0CEF |
laoo | ラオ数字 | ໐ ~ ໙ | U+0ED0 ~ U+0ED9 |
latn | 算用数字 (アラビア数字) | 0 ~ 9 | U+0030 ~ U+0039 |
limb | リンブ数字 | ― | U+1946 ~ U+194F |
mlym | マラヤーラム数字 | ൦ ~ ൯ | U+0D66 ~ U+0D6F |
mong | モンゴル数字 | ᠐ ~ ᠙ | U+1810 ~ U+1819 |
mymr | ミャンマー数字 | ၀ ~ ၉ | U+1040 ~ U+1049 |
orya | オリヤー数字 | 뙦 ~ 뙯 | U+0B66 ~ U+0B6F |
tamldec | タミル数字 | ௦ ~ ௯ | U+0BE6 ~ U+0BEF |
telu | テルグ数字 | ౦ ~ ౯ | U+0C66 ~ U+0C6F |
thai | タイ数字 | ๐ ~ ๙ | U+0E50 ~ U+0E59 |
tibt | チベット数字 | ༠ ~ ༩ | U+0F20 ~ U+0F29 |
options (オプション)
オプション | 値 (太字:デフォルト値) | 説明 |
---|---|---|
localeMatcher | 'lookup':Lookupアルゴリズム 'best fit':最適アルゴリズム | ロケールマッチングアルゴリズム |
numberingSystem | 上記 Unicode 拡張 数値フォーマット の値 (例) 'fullwide':全角数字 'hanidec':漢数字 | ナンバリング システム |
style | 'narrow':縮小形式 'short':短い形式 'long':長い形式 | 形式スタイル |
numeric | 'always':常時数値表現 'auto':特殊表現が無い場合に数値表現 | 数値表現タイプ |
例
format【フォーマット】の 例 も参照
const formatterJ = new Intl.RelativeTimeFormat('ja');
const formatterJA = new Intl.RelativeTimeFormat('ja', { numeric: 'auto' });
for (let value of [-3, -2, -1, 0, 1, 2, 3]) {
console.log(formatterJ.format(value, 'day'), '/', formatterJA.format(value, 'day'));
}
// 出力例:3 日前 / 3 日前
// 出力例:2 日前 / 一昨日
// 出力例:1 日前 / 昨日
// 出力例:0 日後 / 今日
// 出力例:1 日後 / 明日
// 出力例:2 日後 / 明後日
// 出力例:3 日後 / 3 日後
const formatterE = new Intl.RelativeTimeFormat('en');
const formatterEA = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
for (let value of [-2, -1, 0, 1, 2]) {
console.log(formatterE.format(value, 'day'), '/',formatterEA.format(value, 'day'));
}
// 出力例:2 days ago / 2 days ago
// 出力例:1 day ago / yesterday
// 出力例:in 0 days / today
// 出力例:in 1 day / tomorrow
// 出力例:in 2 days / in 2 days
const formatterNon = new Intl.RelativeTimeFormat();
console.log(formatterNon.resolvedOptions());
// 出力例:Object { locale: "ja-JP", style: "long", numeric: "always", numberingSystem: "latn" }
console.log(formatterNon.format(1, 'month'));
// 出力例:1 か月後
const formatterFull = new Intl.RelativeTimeFormat('ja-u-nu-fullwide');
console.log(formatterFull.format(123, 'year'));
// 出力例:123 年後
const formatterKanji = new Intl.RelativeTimeFormat('ja', { numberingSystem: 'hanidec'});
console.log(formatterKanji.format(123, 'year'));
// 出力例:一二三 年後
Intl.RelativeTimeFormat.prototype.format【フォーマット】
メモ
- 値を時間単位でフォーマット
- 全角数字・漢数字等を使用するには、 コンストラクタ の Unicode 拡張 数値フォーマット ・numberingSystem【ナンバリング システム】を参照
- 関連
- 外部リンク (英語)
ECMA-402:Intl.RelativeTimeFormat.prototype.format ( value, unit ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
RelativeTimeFormat.format ( value, unit )
フォーマット文字列
value値
unit時間単位
unit (時間単位)
unit | 説明 |
---|---|
'second' | 秒 |
'minute' | 分 |
'hour' | 時 |
'day' | 日 |
'week' | 週 |
'month' | 月 |
'quarter' | 四半期 |
'year' | 年 |
例
const formatterJ = new Intl.RelativeTimeFormat('ja');
const formatterE = new Intl.RelativeTimeFormat('en');
const formatterJA = new Intl.RelativeTimeFormat('ja', { numeric: 'auto' });
const formatterEA = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(formatterJ.format(-1, 'year'), '/', formatterE.format(-1, 'year'));
// 出力例:1 年前 / 1 year ago
console.log(formatterJ.format(0, 'year'), '/', formatterE.format(0, 'year'));
// 出力例:0 年後 / in 0 years
console.log(formatterJ.format(1, 'year'), '/', formatterE.format(1, 'year'));
// 出力例:1 年後 / in 1 year
console.log(formatterJA.format(-1, 'year'), '/', formatterEA.format(-1, 'year'));
// 出力例:昨年 / last year
console.log(formatterJA.format(0, 'year'), '/', formatterEA.format(0, 'year'));
// 出力例:今年 / this year
console.log(formatterJA.format(1, 'year'), '/', formatterEA.format(1, 'year'));
// 出力例:来年 / next year
console.log(formatterJ.format(-1, 'month'), '/', formatterE.format(-1, 'month'));
// 出力例:1 か月前 / 1 month ago
console.log(formatterJ.format(0, 'month'), '/', formatterE.format(0, 'month'));
// 出力例:0 か月後 / in 0 months
console.log(formatterJ.format(1, 'month'), '/', formatterE.format(1, 'month'));
// 出力例:1 か月後 / in 1 month
console.log(formatterJA.format(-1, 'month'), '/', formatterEA.format(-1, 'month'));
// 出力例:先月 / last month
console.log(formatterJA.format(0, 'month'), '/', formatterEA.format(0, 'month'));
// 出力例:今月 / this month
console.log(formatterJA.format(1, 'month'), '/', formatterEA.format(1, 'month'));
// 出力例:来月 / next month
console.log(formatterJ.format(-1, 'day'), '/', formatterE.format(-1, 'day'));
// 出力例:1 日前 / 1 day ago
console.log(formatterJ.format(0, 'day'), '/', formatterE.format(0, 'day'));
// 出力例:0 日後 / in 0 days
console.log(formatterJ.format(1, 'day'), '/', formatterE.format(1, 'day'));
// 出力例:1 日後 / in 1 day
console.log(formatterJA.format(-1, 'day'), '/', formatterEA.format(-1, 'day'));
// 出力例:昨日 / yesterday
console.log(formatterJA.format(0, 'day'), '/', formatterEA.format(0, 'day'));
// 出力例:今日 / today
console.log(formatterJA.format(1, 'day'), '/', formatterformatterEA.format(1, 'day'));
// 出力例:明日 / tomorrow
function timeFormat(values, unit) {
let formatterNarrow = new Intl.RelativeTimeFormat('ja', { numeric: 'auto', style: 'narrow' });
let formatterShort = new Intl.RelativeTimeFormat('ja', { numeric: 'auto', style: 'short' });
let formatterLong = new Intl.RelativeTimeFormat('ja', { numeric: 'auto', style: 'long' });
for (let value of values) {
let timeNarrow = formatterNarrow.format(value, unit);
let timeShort = formatterShort.format(value, unit);
let timeLong = formatterLong.format(value, unit);
console.log(timeNarrow, '/', timeShort, '/', timeLong);
}
}
timeFormat([-2, -1, 0, 1, 2], 'year');
// 出力例:2年前 / 2 年前 / 2 年前
// 出力例:昨年 / 昨年 / 昨年
// 出力例:今年 / 今年 / 今年
// 出力例:来年 / 来年 / 来年
// 出力例:2年後 / 2 年後 / 2 年後
timeFormat([-2, -1, 0, 1, 2], 'quarter');
// 出力例:2四半期前 / 2 四半期前 / 2 四半期前
// 出力例:前四半期 / 前四半期 / 前四半期
// 出力例:今四半期 / 今四半期 / 今四半期
// 出力例:翌四半期 / 翌四半期 / 翌四半期
// 出力例:2四半期後 / 2 四半期後 / 2 四半期後
timeFormat([-2, -1, 0, 1, 2], 'month');
// 出力例:2か月前 / 2 か月前 / 2 か月前
// 出力例:先月 / 先月 / 先月
// 出力例:今月 / 今月 / 今月
// 出力例:来月 / 来月 / 来月
// 出力例:2か月後 / 2 か月後 / 2 か月後
timeFormat([-2, -1, 0, 1, 2], 'week');
// 出力例:2週間前 / 2 週間前 / 2 週間前
// 出力例:先週 / 先週 / 先週
// 出力例:今週 / 今週 / 今週
// 出力例:来週 / 来週 / 来週
// 出力例:2週間後 / 2 週間後 / 2 週間後
timeFormat([-3, -2, -1, 0, 1, 2, 3], 'day');
// 出力例:3日前 / 3 日前 / 3 日前
// 出力例:一昨日 / 一昨日 / 一昨日
// 出力例:昨日 / 昨日 / 昨日
// 出力例:今日 / 今日 / 今日
// 出力例:明日 / 明日 / 明日
// 出力例:明後日 / 明後日 / 明後日
// 出力例:3日後 / 3 日後 / 3 日後
timeFormat([-1, 0, 1], 'hour');
// 出力例:1時間前 / 1 時間前 / 1 時間前
// 出力例:1 時間以内 / 1 時間以内 / 1 時間以内
// 出力例:1時間後 / 1 時間後 / 1 時間後
timeFormat([-1, 0, 1], 'minute');
// 出力例:1分前 / 1 分前 / 1 分前
// 出力例:1 分以内 / 1 分以内 / 1 分以内
// 出力例:1分後 / 1 分後 / 1 分後
timeFormat([-1, 0, 1], 'second');
// 出力例:1秒前 / 1 秒前 / 1 秒前
// 出力例:今 / 今 / 今
// 出力例:1秒後 / 1 秒後 / 1 秒後
Intl.RelativeTimeFormat.prototype.formatToParts【フォーマットパーツ取得】
メモ
- フォーマットされたリストをパーツに分解
- パーツを編集し組み合わせることで、独自フォーマットが可能
- 関連
- 外部リンク (英語)
ECMA-402:Intl.RelativeTimeFormat.prototype.formatToParts ( value, unit ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
例
const formatterJ = new Intl.RelativeTimeFormat('ja');
const formatterE = new Intl.RelativeTimeFormat('en');
const formatterJA = new Intl.RelativeTimeFormat('ja', { numeric: 'auto' });
const formatterEA = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
const values = [-1, 0, 1];
for (let value of values) {
let array = formatterJ.formatToParts(value, 'year');
for (let i = 0; i < array.length; i++) {
console.log(i, array[i]);
}
console.log();
}
// 出力例:
// 0 Object { type: "integer", value: "1", unit: "year" }
// 1 Object { type: "literal", value: " 年前" }
//
// 0 Object { type: "integer", value: "0", unit: "year" }
// 1 Object { type: "literal", value: " 年後" }
//
// 0 Object { type: "integer", value: "1", unit: "year" }
// 1 Object { type: "literal", value: " 年後" }
for (let value of values) {
let array = formatterJA.formatToParts(value, 'year');
for (let i = 0; i < array.length; i++) {
console.log(i, array[i]);
}
console.log();
}
// 出力例:
// 0 Object { type: "literal", value: "昨年" }
//
// 0 Object { type: "literal", value: "今年" }
//
// 0 Object { type: "literal", value: "来年" }
for (let value of values) {
let array = formatterE.formatToParts(value, 'year');
for (let i = 0; i < array.length; i++) {
console.log(i, array[i]);
}
console.log();
}
// 出力例:
// 0 Object { type: "integer", value: "1", unit: "year" }
// 1 Object { type: "literal", value: " year ago" }
//
// 0 Object { type: "literal", value: "in " }
// 1 Object { type: "integer", value: "0", unit: "year" }
// 2 Object { type: "literal", value: " years" }
//
// 0 Object { type: "literal", value: "in " }
// 1 Object { type: "integer", value: "1", unit: "year" }
// 2 Object { type: "literal", value: " year" }
for (let value of values) {
let array = formatterEA.formatToParts(value, 'year');
for (let i = 0; i < array.length; i++) {
console.log(i, array[i]);
}
console.log();
}
// 出力例:
// 0 Object { type: "literal", value: "last year" }
//
// 0 Object { type: "literal", value: "this year" }
//
// 0 Object { type: "literal", value: "next year" }
Intl.RelativeTimeFormat.prototype.resolvedOptions【オプション取得】
メモ
- ロケール・オプションを取得
- 指定したプロパティを持ったオプション オブジェクトを取得
- 関連
- 外部リンク (英語)
ECMA-402:Intl.RelativeTimeFormat.prototype.resolvedOptions ( ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
RelativeTimeFormat.resolvedOptions ()
オブジェクト (下記プロパティ有効:詳細は コンストラクタ 参照)
プロパティ | 説明 |
---|---|
locale | ロケール |
style | 形式スタイル |
numeric | 数値表現タイプ |
numberingSystem | ナンバリング システム |
例
const formatterJ = new Intl.RelativeTimeFormat('ja', { numeric: 'auto', numberingSystem: 'fullwide' });
console.log(formatterJ.resolvedOptions());
// 出力例:Object { locale: "ja", style: "long", numeric: "auto", numberingSystem: "fullwide" }
const formatterE = new Intl.RelativeTimeFormat('en', { style: 'narrow' });
console.log(formatterE.resolvedOptions());
// 出力例:Object { locale: "en", style: "narrow", numeric: "always", numberingSystem: "latn" }
Intl.RelativeTimeFormat.supportedLocalesOf【サポート ロケール取得】
メモ
- 指定したロケールから、サポート対象のロケールを取得
- 関連
- 外部リンク (英語)
ECMA-402:Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
例
console.log(Intl.RelativeTimeFormat.supportedLocalesOf('ja'));
// 出力例:Array [ "ja" ]
console.log(Intl.RelativeTimeFormat.supportedLocalesOf('ng'));
// 出力例:Array []
let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]
locales = ['ng', 'jpn', 'jpn-JP', 'eng', 'eng-US', 'eng-GB'];
console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]
console.log(Intl.RelativeTimeFormat.supportedLocalesOf(locales, { localeMatcher: 'lookup' }));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]