時間差計算/ 年 · 月 · 日 · 時 · 分

2つの日付または時刻の正確な差を計算 — 年、月、日、時、分、秒に分解し、総日数と総週数も表示。

開始
終了
開始から終了までの時間
1
0
0
0
時間
0
0
365
合計日数
52
合計週数
8,760
合計時間
525,600
合計分

このツールは役に立ちましたか?

コード例

JavaScript (date-fns)
import {
  differenceInYears, differenceInMonths,
  differenceInDays, differenceInHours,
  formatDistanceToNow, intervalToDuration,
  format
} from 'date-fns';

const start = new Date('2020-01-15');
const end = new Date('2024-06-30');

differenceInYears(end, start)   // 4
differenceInDays(end, start)    // 1628
differenceInHours(end, start)   // 39072

// Human-readable breakdown
intervalToDuration({ start, end })
// { years:4, months:5, days:15, hours:0, ... }

// Relative time
formatDistanceToNow(start, { addSuffix: true })
// "about 4 years ago"
Python
from datetime import datetime, date
from dateutil.relativedelta import relativedelta

start = datetime(2020, 1, 15)
end = datetime(2024, 6, 30)

# Total difference
diff = end - start
diff.days          # 1628
diff.total_seconds() / 3600  # 39072.0

# Calendar-aware breakdown (pip install python-dateutil)
delta = relativedelta(end, start)
delta.years    # 4
delta.months   # 5
delta.days     # 15

# Age calculation
birthday = date(1990, 5, 20)
today = date.today()
age = relativedelta(today, birthday).years
Go
import (
  "fmt"
  "time"
)

start := time.Date(2020, 1, 15, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, 6, 30, 0, 0, 0, 0, time.UTC)

duration := end.Sub(start)
fmt.Println(duration.Hours() / 24)  // 1628 days
fmt.Println(duration.Hours())       // 39072 hours

// Calendar diff (years/months) — manual calculation
years := end.Year() - start.Year()
months := int(end.Month()) - int(start.Month())
if months < 0 {
    years--
    months += 12
}
fmt.Printf("%d years, %d months\n", years, months)
SQL
-- MySQL / MariaDB
SELECT
  DATEDIFF('2024-06-30', '2020-01-15') AS days,
  TIMESTAMPDIFF(YEAR,  '2020-01-15', '2024-06-30') AS years,
  TIMESTAMPDIFF(MONTH, '2020-01-15', '2024-06-30') AS months,
  TIMESTAMPDIFF(HOUR,  '2020-01-15', '2024-06-30') AS hours;

-- PostgreSQL
SELECT
  '2024-06-30'::date - '2020-01-15'::date AS days,
  AGE('2024-06-30', '2020-01-15') AS human_readable;
  -- 4 years 5 mons 15 days

-- SQLite
SELECT julianday('2024-06-30') - julianday('2020-01-15');
-- 1628.0

よくある質問

なぜミリ秒の単純除算では年/月/日が得られないのですか?
ミリ秒の除算は正確な総時間を提供しますが、直感的な「X年Y月Z日」の内訳は得られません。月の長さは異なる(28〜31日)ため、暦を考慮したアルゴリズムを使用しています。
年齢や勤続年数を計算するには?
生年月日または開始日を「開始」として、現在の日付を「終了」として入力すると、正確な年数(残りの月と日を含む)が得られます。
2つの日付間の営業日数は?
営業日計算は複雑です:週末を除外し、オプションで休日も除外します。本番環境では date-fns の differenceInBusinessDays などのライブラリを使用することをお勧めします。
う秒は時間差計算に影響しますか?
通常の日付計算では影響しません。JavaScript の Date オブジェクトとほとんどの OS はう秒を考慮しません。極めて正確なタイミング測定でのみ重要です。
JavaScript で相対時間(「3日前」など)を表示するには?
Intl.RelativeTimeFormat API(モダンブラウザでネイティブサポート)を使用:const rtf = new Intl.RelativeTimeFormat('ja', { numeric: 'auto' }); rtf.format(-3, 'day')。または date-fns の formatDistanceToNow() を使用。
異なるタイムゾーン間の時間差を計算するには?
JavaScript の Date オブジェクトは内部で UTC タイムスタンプを保存し、表示時にのみタイムゾーン情報が適用されます。getTime() の差を取れば、タイムゾーンに関係なく正しい差が得られます。