JavaScript

Intl.DateTimeFormat.prototype.formatRange() メソッド


編集:2022.12.14 by T.A.

[Edge]。→

Intl.DateTimeFormat により指定の日時範囲に書式化した文字列を返します。

構文
Intl.DateTimeFormat.formatRange(startDate, endDate)
Intl.DateTimeFormat Intl.DateTimeFormat
startDate ¶開始日時を表す Date または数値(協定世界時(UTC)での 1970年1月1日 00:00:00から経過のミリ秒)。
endDate ¶終了日時を表す Date または数値(協定世界時(UTC)での 1970年1月1日 00:00:00から経過のミリ秒)。

startDateendDate はどちらが大きくても小さくても構わないようです。

戻り値

日時の範囲を示す文字列。

//●指定の仕方
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long', timeStyle:'long'});  //日付、時刻。
D=DA.formatRange(0, 1000);  //協定世界時(UTC)での 1970年1月1日 00:00:00から経過ミリ秒で指定。
	//=[string]:1970/1/1 9:00:00 JST~9:00:01 JST
D=DA.formatRange(new Date('2020-10-05T19:23:30'), new Date('2022-10-05T10:05:30'));
	//=[string]:2020/10/5 19:23:30 JST~2022/10/5 10:05:30 JST
D=DA.formatRange(new Date('2022-10-05T10:05:30'), new Date('2020-10-05T19:23:30'));
	//=[string]:2022/10/5 10:05:30 JST~2020/10/5 19:23:30 JST
	//startDateendDate のどちらが大きくてもOKのようです。

//●Intl.DateTimeFormat の指定による色々
//○日付を設定しておく
DT1=new Date('2020-10-05T09:20:30.100');
DT2=new Date('2020-10-06T09:35:30.200');

DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long'});  //日付。
D=DA.formatRange(DT1, DT2);  //=[string]:2020/10/05~2020/10/06
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long', timeStyle:'long'});  //日付、時刻。
D=DA.formatRange(DT1, DT2);  //=[string]:2020/10/5 9:20:30 JST~2020/10/6 9:35:30 JST
DA=new Intl.DateTimeFormat('ja-JP', {fractionalSecondDigits:3});  //ミリ秒。
D=DA.formatRange(DT1, DT2);  //=[string]:2020/10/5 100~2020/10/6 200

Intl.DateTimeFormat() コンストラクター


テスト