JavaScriptで日付をフォーマット、ライブラリ未使用

JavaScriptで日付をフォーマットする方法です。

momentなどのライブラリを使用せずにフォーマットする方法です。

自由に好き勝手にフォーマットすることはできませんが、通常使いならイケるイケる!

JavaScriptで日付をフォーマット

toLocaleDateString()を使用します。

localesは日本語はja-JP、英語表記ならen-USを利用します。

console.log(new Date().toLocaleDateString('ja-JP', { dateStyle:"full" }));
console.log(new Date().toLocaleDateString('ja-JP', { dateStyle:"long" }));
console.log(new Date().toLocaleDateString('ja-JP', { dateStyle:"medium" }));
console.log(new Date().toLocaleDateString('ja-JP', { dateStyle:"short" }));
console.log(new Date().toLocaleDateString('ja-JP', { weekday:"short", year:"numeric", month:"short", day:"numeric"}));
console.log(new Date().toLocaleDateString('ja-JP', { weekday:"short", month:"2-digit", day:"2-digit"}));

出力は上から下記のようになります。

2023年6月10日土曜日
2023年6月10日
2023/06/10
2023/06/10
2023年6月10日(土)
06/10(土)

曜日まで日本語表記で表示できます、ライブラリなしでもなんとかなりそうです。

関連記事
最新記事