JavaScript

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


編集:2023.01.09 by T.A.

[Edge]。→

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

構文
Intl.DateTimeFormat.format(date)
Intl.DateTimeFormat Intl.DateTimeFormat
date 日時を表す Date または¶数値(協定世界時(UTC)での 1970年1月1日 00:00:00から経過のミリ秒)。
戻り値

文字列。

//●date の指定方法
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long'});
D=DA.format(new Date());  //現在の日時で。
	//=[string]:2022年12月6日
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long'});
D=DA.format('2020-10-05');  //日付文字列で。
	//エラー発生:Invalid time value
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long', timeStyle:'long'});
D=DA.format(0);     //数値で。
	//=[string]:1970年1月1日 9:00:00 JST
D=DA.format(1000);  //数値で。
	//=[string]:1970年1月1日 9:00:01 JST

//●
DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long'});  //日付。
D=DA.format(new Date('2020-10-05T19:23:30.123'));  //
	//=[string]:2020年10月5日

DA=new Intl.DateTimeFormat('ja-JP', {dateStyle:'long', timeStyle:'long', fractionalSecondDigits:3});
	//日付、時刻、ミリ秒。
D=DA.format(new Date('2020-10-05T19:23:30.123'));
	//エラー発生:Can't set option fractionalSecondDigits when dateStyle is used

DA=new Intl.DateTimeFormat('ja-JP', {timeStyle:'long', fractionalSecondDigits:3});
	//時刻、ミリ秒。
D=DA.format(new Date('2020-10-05T19:23:30.123'));
	//エラー発生:Can't set option fractionalSecondDigits when timeStyle is used

DA=new Intl.DateTimeFormat('ja-JP', {fractionalSecondDigits:3});  //「ミリ秒」部を3桁
D=DA.format(new Date('2020-10-05T19:23:30.123'));  //=[string]:123

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


テスト