Widely available すべての主要ブラウザで対応済み。安心して使用可能。

概要

Intl.RelativeTimeFormat は「3日前」「2か月後」のような相対的な時間表現をロケールに応じて生成します。

対応ブラウザ

機能 デスクトップ モバイル
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
71
79
65
14
71
14
ビルトインオブジェクト

Intl.RelativeTimeFormat() コンストラクターは、 Intl/RelativeTimeFormat オブジェクトを生成します。

71
79
65
14
71
14
Intl.RelativeTimeFormat.RelativeTimeFormat.locales parameter

「ロケール」パラメータ

71
79
65
14
71
14
Intl.RelativeTimeFormat.RelativeTimeFormat.options parameter.options localeMatcher parameter

`options.localeMatcher` パラメータ

71
79
65
14
71
14
Intl.RelativeTimeFormat.RelativeTimeFormat.options parameter.options numberingSystem parameter

`options.numberingSystem` パラメータ

71
79
76
14
71
14
Intl.RelativeTimeFormat.RelativeTimeFormat.options parameter.options numeric parameter

`options.numeric` パラメータ

71
79
65
14
71
14

Intl.RelativeTimeFormat.prototype.format() メソッドは value や unit を、この Intl.RelativeTimeFormat オブジェクトのロケールと整形オプションに従って整形します。

71
79
65
14
71
14

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

71
79
70
14
71
14

Intl.RelativeTimeFormat インスタンスのsolvedOptions() メソッドは、この RelativeTimeFormat オブジェクトの初期化中に計算されたオプションを反映するプロパティを持つ新しいオブジェクトを返します。

71
79
65
14
71
14

Intl.RelativeTimeFormat.supportedLocalesOf() 静的メソッドは、ランタイムのデフォルト ロケールにフォールバックすることなく、相対時間形式でサポートされている指定されたロケールを含む配列を返します。

71
79
65
14
71
14
1+対応 (バージョン) 未対応 注釈あり サブ機能の解説は MDN Web Docs (CC BY-SA 2.5)

基本構文

JAVASCRIPT
const rtf = new Intl.RelativeTimeFormat('ja', { numeric: 'auto' });
rtf.format(-1, 'day');   // 'Yesterday'
rtf.format(2, 'hour');   // '2 hours from now'
rtf.format(-3, 'month'); // '3 months ago'

ライブデモ

Intl.RelativeTimeFormat

"3before""2 timebetweenafter". like relative timebetweenrepresentationationation.

プレビュー全画面表示

Numeric option

Auto is "", always is "1before". like to output.

プレビュー全画面表示

datetime. Display

Date and combination"Nsplitbefore". like display implementation.

プレビュー全画面表示

実務での使いどころ

  • Intl.RelativeTimeFormat の活用

    相対的な時間表現(「3日前」「2時間後」)をロケールに応じてフォーマットする API。

注意点

  • 特になし。すべての主要ブラウザで安定して動作する。

アクセシビリティ

  • JavaScript による動的更新時は、aria-live リージョンで変更をスクリーンリーダーに通知する。

Powered by web-features