JavaScript で日付を扱うには Date オブジェクトを使います。現在の年月日や時刻を取得するのはもちろん、特定の日付の作成やフォーマット変換もこのオブジェクトが基本です。
本記事では、Date の生成方法、年月日時分秒を取得する全メソッド、YYYY-MM-DD 形式へのフォーマット、toLocaleString / Intl.DateTimeFormat、タイムスタンプまで解説します。
・new Date() で現在の日時を取得する方法
・getFullYear / getMonth / getDate 等の全メソッド一覧
・getMonth() が 0 始まり(0 = 1 月)の注意点
・YYYY-MM-DD / YYYY/MM/DD 形式へのフォーマット
・padStart() によるゼロ埋め
・toLocaleString / Intl.DateTimeFormat での日本語フォーマット
・Date.now() でタイムスタンプを取得する方法
・Temporal API(次世代の日付 API)の概要
Date オブジェクトの生成
// 現在の日時
const now = new Date();
console.log(now); // Mon Mar 30 2026 14:30:00 GMT+0900 (JST)
// 文字列から生成
const d1 = new Date("2026-03-30"); // ISO 8601 形式(推奨)
const d2 = new Date("2026-03-30T14:30:00"); // 日時指定
const d3 = new Date("2026/03/30 14:30:00"); // スラッシュ区切り
// 数値で指定(月は 0 始まり: 0=1月, 11=12月)
const d4 = new Date(2026, 2, 30, 14, 30, 0); // 2026年3月30日 14:30:00
// 年 月 日 時 分 秒
// ↑ 2 = 3月(0始まり)
// タイムスタンプ(ミリ秒)から生成
const d5 = new Date(1775088600000);
new Date(2026, 2, 30) の第 2 引数 2 は3 月です(0 = 1 月)。これは JavaScript の Date で最もよくある間違いです。getMonth() も同様に 0〜11 を返すため、表示時には +1 が必要です。年月日・時分秒を取得するメソッド一覧
| メソッド | 戻り値 | 例(2026-03-30 14:05:09) |
|---|---|---|
| getFullYear() | 4 桁の年 | 2026 |
| getMonth() | 月(0〜11) | 2(3 月) |
| getDate() | 日(1〜31) | 30 |
| getDay() | 曜日(0=日〜6=土) | 1(月曜) |
| getHours() | 時(0〜23) | 14 |
| getMinutes() | 分(0〜59) | 5 |
| getSeconds() | 秒(0〜59) | 9 |
| getMilliseconds() | ミリ秒(0〜999) | 0 |
| getTime() | Unix タイムスタンプ(ミリ秒) | 1775088309000 |
| getTimezoneOffset() | UTC との差(分) | -540(JST = UTC+9 = -540 分) |
const now = new Date();
const year = now.getFullYear(); // 2026
const month = now.getMonth() + 1; // 3(+1 を忘れずに)
const date = now.getDate(); // 30
const day = now.getDay(); // 1(月曜)
const hours = now.getHours(); // 14
const minutes = now.getMinutes(); // 5
const seconds = now.getSeconds(); // 9
console.log(`${year}/${month}/${date} ${hours}:${minutes}:${seconds}`);
// "2026/3/30 14:5:9"(ゼロ埋めなし)
紛らわしいですが、
getDate() は月の日(1〜31)、getDay() は曜日(0=日曜〜6=土曜)です。「何日」を取得するのに getDay() を使う間違いが非常に多いので注意してください。YYYY-MM-DD / YYYY/MM/DD 形式にフォーマットする
// ゼロ埋め関数(padStart を活用)
function formatDate(date, separator = "-") {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}${separator}${m}${separator}${d}`;
}
const now = new Date();
console.log(formatDate(now)); // "2026-03-30"
console.log(formatDate(now, "/")); // "2026/03/30"
function formatDateTime(date) {
const y = date.getFullYear();
const mo = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
const h = String(date.getHours()).padStart(2, "0");
const mi = String(date.getMinutes()).padStart(2, "0");
const s = String(date.getSeconds()).padStart(2, "0");
return `${y}-${mo}-${d} ${h}:${mi}:${s}`;
}
console.log(formatDateTime(new Date()));
// "2026-03-30 14:05:09"
日付フォーマットの詳しいパターンは「日付のフォーマットを変換する方法」「YYYY/MM/DD 形式で表示する方法」を参照してください。
toLocaleString / Intl.DateTimeFormat で日本語フォーマット
const now = new Date();
// 日本語ロケール
console.log(now.toLocaleString("ja-JP"));
// "2026/3/30 14:05:09"
console.log(now.toLocaleDateString("ja-JP"));
// "2026/3/30"
console.log(now.toLocaleTimeString("ja-JP"));
// "14:05:09"
// オプションでフォーマットを制御
console.log(now.toLocaleDateString("ja-JP", {
year: "numeric",
month: "long",
day: "numeric",
weekday: "long",
}));
// "2026年3月30日月曜日"
// Intl.DateTimeFormat で再利用可能なフォーマッターを作成
const fmt = new Intl.DateTimeFormat("ja-JP", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
console.log(fmt.format(new Date()));
// "2026/03/30 14:05:09"
// 元号(和暦)
const wareki = new Intl.DateTimeFormat("ja-JP-u-ca-japanese", {
era: "long",
year: "numeric",
month: "long",
day: "numeric",
});
console.log(wareki.format(new Date()));
// "令和8年3月30日"
toLocaleString の出力はブラウザや OS のロケール設定に依存します。全環境で同じ出力が必要な場合は、getFullYear() 等で手動フォーマットするか、Intl.DateTimeFormat でロケールを明示指定してください。Intl API の詳細は「Intl API を使った日付・数値の国際化フォーマット」を参照してください。
ISO 8601 形式の文字列を取得する
const now = new Date(); // ISO 8601 形式(UTC) console.log(now.toISOString()); // "2026-03-30T05:05:09.000Z"(UTC 時刻。日本時間 - 9 時間) // toJSON は toISOString と同じ結果 console.log(now.toJSON()); // "2026-03-30T05:05:09.000Z" // 日付部分だけ(YYYY-MM-DD) console.log(now.toISOString().slice(0, 10)); // "2026-03-30"(ただし UTC 基準なので日本時間とずれる場合あり)
日本時間(JST = UTC+9)の 2026-03-30 02:00 は、UTC では 2026-03-29 17:00 になります。
toISOString().slice(0, 10) で日付を取ると日本時間と 1 日ずれる場合があります。ローカル時間の YYYY-MM-DD が必要なら getFullYear() 等で手動フォーマットしてください。タイムスタンプの取得
// 現在のタイムスタンプ(ミリ秒: 1970-01-01 00:00:00 UTC からの経過時間)
const ts1 = Date.now(); // 静的メソッド(最も高速)
const ts2 = new Date().getTime(); // インスタンスメソッド
const ts3 = +new Date(); // 単項 + 演算子で数値変換
console.log(ts1); // 1775088309000
// 秒単位のタイムスタンプ(Unix 時間: サーバー API でよく使う)
const unixTime = Math.floor(Date.now() / 1000);
console.log(unixTime); // 1775088309
// タイムスタンプから Date に変換
const fromTs = new Date(1775088309000);
console.log(fromTs.toLocaleString("ja-JP"));
| 方法 | 速度 | 備考 |
|---|---|---|
| Date.now() | 最速 | Date オブジェクトを生成しない。パフォーマンス計測に最適 |
| new Date().getTime() | 普通 | Date オブジェクトを生成する |
| +new Date() | 普通 | 短く書けるが可読性が低い |
曜日を取得する
const WEEKDAYS_JP = ["日", "月", "火", "水", "木", "金", "土"];
const WEEKDAYS_EN = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const now = new Date();
const dayIndex = now.getDay(); // 0=日曜〜6=土曜
console.log(WEEKDAYS_JP[dayIndex]); // "月"
console.log(WEEKDAYS_EN[dayIndex]); // "Mon"
// toLocaleDateString で曜日を取得(ブラウザ API)
console.log(now.toLocaleDateString("ja-JP", { weekday: "long" }));
// "月曜日"
Temporal API(次世代の日付 API)
Temporal は JavaScript の Date の問題(月が 0 始まり、タイムゾーンの扱い等)を解決する次世代の日付 API です。2026 年現在 Stage 3 で、一部ブラウザで実験的に利用可能です。
// ※ Temporal は Stage 3。ポリフィルまたは対応ブラウザが必要
// 現在の日付(タイムゾーン付き)
// const now = Temporal.Now.plainDateISO();
// console.log(now.toString()); // "2026-03-30"
// console.log(now.year); // 2026
// console.log(now.month); // 3(1 始まり!)
// console.log(now.day); // 30
// 日付の計算
// const nextWeek = now.add({ days: 7 });
// const diff = now.until(nextWeek);
// console.log(diff.days); // 7
| Date の問題 | Temporal の解決 |
|---|---|
| 月が 0 始まり | 1 始まり(1 = 1 月) |
| ミュータブル(変更可能) | イミュータブル(変更不可。新しいオブジェクトを返す) |
| タイムゾーンの扱いが曖昧 | PlainDate / ZonedDateTime で明確に区別 |
| 日付計算が面倒 | add() / subtract() / until() メソッドで直感的 |
Temporal は 2026 年時点でまだ Stage 3 であり、全ブラウザでの正式サポートはされていません。本番コードでは Date を使い、day.js や date-fns などのライブラリで補完するのが現実的です。Temporal の正式リリース後に移行を検討してください。
実務パターン集
パターン(1): 今日の日付を input[type=”date”] にセット
// input の value は YYYY-MM-DD 形式が必要
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, "0");
const dd = String(today.getDate()).padStart(2, "0");
document.querySelector('input[type="date"]').value = `${yyyy}-${mm}-${dd}`;
パターン(2): 表示用の日本語日付
const now = new Date();
const DAYS = ["日", "月", "火", "水", "木", "金", "土"];
const formatted = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日(${DAYS[now.getDay()]})`;
console.log(formatted); // "2026年3月30日(月)"
パターン(3): コピーライトの年を自動更新
<!-- HTML -->
<footer>© <span id="year"></span> MyCompany</footer>
<!-- JavaScript -->
<script>
document.getElementById("year").textContent = new Date().getFullYear();
</script>
<!-- 出力: © 2026 MyCompany -->
パターン(4): API に送る ISO 形式の日時
// API リクエストに含める日時
const payload = {
title: "新しい記事",
createdAt: new Date().toISOString(), // UTC の ISO 8601
};
console.log(payload.createdAt); // "2026-03-30T05:05:09.000Z"
パターン(5): 処理時間の計測
// Date.now() で処理時間を計測
const start = Date.now();
// 計測対象の処理
heavyProcess();
const elapsed = Date.now() - start;
console.log(`処理時間: ${elapsed}ms`);
// より精密な計測は performance.now() を使う
const t0 = performance.now();
heavyProcess();
const t1 = performance.now();
console.log(`処理時間: ${(t1 - t0).toFixed(2)}ms`);
よくある質問
getMonth() は0 始まり(0 = 1 月, 11 = 12 月)です。表示時には必ず +1 してください。getMonth() + 1 が正しい月になります。これは JavaScript の仕様であり、バグではありません。getDate() は「月の日」(1〜31)を返します。getDay() は「曜日」(0=日曜〜6=土曜)を返します。「今日は何日?」は getDate()、「今日は何曜日?」は getDay() です。toISOString() はUTC 基準です。日本時間の深夜 0:00〜8:59 は UTC では前日になるため、日付が 1 日ずれて見えます。ローカル時間が必要なら getFullYear() 等で手動フォーマットしてください。date.setDate(date.getDate() + N) で N 日後の Date が得られます。月またぎも自動的に処理されます。詳細は「日時の計算をマスターしよう」「日時の差分を計算する」を参照。Date.now() は Date オブジェクトを生成しないためわずかに高速です。パフォーマンス計測やID生成には Date.now() を推奨します。まとめ
JavaScript での日付取得の要点をまとめます。
| やりたいこと | 方法 |
|---|---|
| 現在の日時を取得 | const now = new Date() |
| 年を取得 | now.getFullYear() |
| 月を取得(1〜12) | now.getMonth() + 1 |
| 日を取得 | now.getDate() |
| 曜日を取得(0=日〜6=土) | now.getDay() |
| 時・分・秒を取得 | getHours() / getMinutes() / getSeconds() |
| YYYY-MM-DD にフォーマット | getFullYear() + padStart(2,”0″) で手動組み立て |
| 日本語の日付フォーマット | toLocaleDateString(‘ja-JP’, options) |
| タイムスタンプ(ミリ秒) | Date.now() |
| タイムスタンプ(秒: Unix 時間) | Math.floor(Date.now() / 1000) |
| ISO 8601 文字列(UTC) | now.toISOString() |
フォーマット変換の詳細は「日付のフォーマットを変換する方法」、差分計算は「日時の差分を計算する」、Intl API は「Intl API で日付・数値を国際化」も併せて参照してください。

