手動フォーマット
最も基本的な方法は、Dateオブジェクトのメソッドを使って手動でフォーマットする方法です。
const date = new Date();
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2); // ゼロ埋め
const day = ('0' + date.getDate()).slice(-2); // ゼロ埋め
const formattedDate = `${year}-${month}-${day}`;
console.log(formattedDate); // 例: 2024-12-28
toLocaleDateString()
ロケールに応じた日付表示が簡単にできるメソッドです。
const date = new Date();
// 基本的な使い方
console.log(date.toLocaleDateString('ja-JP')); // 例: 2024/12/28
// オプションでカスタマイズ
const options = { 
  year: 'numeric', 
  month: '2-digit', 
  day: '2-digit' 
};
console.log(date.toLocaleDateString('ja-JP', options)); // 例: 2024/12/28
Intl.DateTimeFormat
最も柔軟で強力な国際化対応の日付フォーマット機能です。
const date = new Date();
// 詳細な日付表示
const formatter = new Intl.DateTimeFormat('ja-JP', { 
  dateStyle: 'full' 
});
console.log(formatter.format(date)); // 例: 2024年12月28日土曜日
// カスタムフォーマット
const customFormatter = new Intl.DateTimeFormat('ja-JP', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  weekday: 'short'
});
console.log(customFormatter.format(date)); // 例: 2024年12月28日(土)
