0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】日付をフォーマットする方法

Posted at

手動フォーマット

最も基本的な方法は、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日(土)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?