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】日時操作をDateとTemporal 同じ処理で比較してみる

0
Posted at

はじめに

JavaScriptのTemporalとは、比較的新しく追加された日時操作のためのAPIです。(2026年3月ごろに正式に標準仕様になったそうです)
今回は、JavaScriptで日時を扱う際に元々使われてきたDateと、Temporalを比較し、コードなどがどのように変わるのかに着目してまとめてみました。

1. 現在日時を取得する

// Date
const now = new Date(); // Sat Oct 03 2026 10:00:00 GMT+0900

// Temporal
const instant = Temporal.Now.instant(); // 2026-10-03T01:00:00.000Z

2. 年・月・日を取得する

// Date
const date = new Date();

const year = date.getFullYear();
const month = date.getMonth() + 1; // 月の値の範囲は 0~11 のため+1が必要
const day = date.getDate();

// Temporal
const date = Temporal.Now.plainDateISO();

const year = date.year;
const month = date.month; // 1~12
const day = date.day;

DateのgetMonth()で得る値は0~11だったため、実際の月に合わせるのに+1をしないといけなかったのですが、Temporalのmonthの値は1~12なので、+1は不要になります。
また、以下のように日付を指定するような場合でも、実際の月から-1をする必要はなくなっているようです。

// Date
const date = new Date(2026, 9, 3); // 月: 10 - 1 = 9
// ↑↓ どちらも10月を表す
// Temporal
const date = Temporal.PlainDate.from("2026-10-03");

3. 日付に1日追加する

// Date
const date = new Date();
date.setDate(date.getDate() + 1);

// Temporal
const date = Temporal.Now.plainDateISO();
const tomorrow = date.add({
  days: 1
});

処理が分かりやすくなっているのもそうですが、Dateのときはdate変数の値自体が変わっていたのに対し、Temporalはimmutableなので新たな変数が作られる形になっています。

4. 日付を比較する

// Date
const date1 = new Date("2026-10-03");
const date2 = new Date("2026-10-10");
if (date1 < date2) {
  console.log("date1の方が早い");
}

// Temporal
const date1 = Temporal.PlainDate.from("2026-10-03");
const date2 = Temporal.PlainDate.from("2026-10-10");
if (Temporal.PlainDate.compare(date1, date2) < 0) {
  console.log("date1の方が早い");
}

以下のように、同じ日か確認する場合も、Temporalなら.equals()を使って直接比較できます。
Dateのときは.getTime()、.getMonth()、.getDate()などを使用し、変換したもので比較する必要がありました。

// Temporal
if (date1.equals(date2)) {
  // 同じ日付ならここが通る
  console.log("同じ日");
}

5. 「2つの日付の差」を求める

2026-10-03と、2026-10-10 の差を求める場合

// Date
const date1 = new Date("2026-10-03");
const date2 = new Date("2026-10-10");

const diff = date2.getTime() - date1.getTime();
const days = diff / (1000 * 60 * 60 * 24);

console.log(days); // 結果:7

// Temporal
const date1 = Temporal.PlainDate.from("2026-10-03");
const date2 = Temporal.PlainDate.from("2026-10-10");

const duration = date1.until(date2);

console.log(duration.days); // 結果:7

6. 時刻だけを扱う

// Date
  // Dateには「時刻だけ」という型がなく、不要な年月日もついてくる。

// Temporal
const time = Temporal.PlainTime.from("14:30");
console.log(time.hour);   // 14
console.log(time.minute); // 30

7. 「月末」を求める

2026年10月の末日を求める場合

// Date
const date = new Date(2026, 10, 0); // 翌月の0日 = 今月の末日になる
console.log(date.getDate()); // 31

// Temporal
const date = Temporal.PlainDate.from("2026-10-03");
const endOfMonth =
  date.with({
    // daysInMonth は「その月に何日間あるか」を返すプロパティ
    day: date.daysInMonth
  });
console.log(endOfMonth.toString()); // 2026-10-31

8. 曜日を取得する

// Date
const date = new Date();
const dayOfWeek = date.getDay();
// 0 = 日曜日、 1 = 月曜日、・・・、 6 = 土曜日

// Temporal
const date = Temporal.Now.plainDateISO();
console.log(date.dayOfWeek);
// 1 = 月曜日、 2 = 火曜日、・・・、 7 = 日曜日

月のときと同様に、始めの数値が0から1に変わるのですが、それだけでなく、始めの曜日も日曜から月曜に変わります。

まとめ

以上が、DateとTemporalの比較になります。
ここで記載したもの以外に、タイムゾーン周りの扱いも色々できるように変わっているので、ぜひ調べてみてください。
参考:https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Temporal

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?