【Python】timedelta

はじめに

経過時間を表す型である「timedelta」について見ていこう。

timeとdelta

timedeltaは、時間を表すtimeと差を表すdeltaが合わさった単語。

サンプルコード

timedelta · ki-hi-ro/python@b7cfb36

from datetime import datetime, timedelta

now = datetime.now()

after_30_days = now + timedelta(days=30)
after_10_hours = now + timedelta(hours=10)
after_5_minutes = now + timedelta(minutes=5)
after_1_second = now + timedelta(seconds=1)

print("Current date and time:", now)
print("Date and time after 30 days:", after_30_days)
print("Date and time after 10 hours:", after_10_hours)
print("Date and time after 5 minutes:", after_5_minutes)
print("Date and time after 1 second:", after_1_second)

出力結果

Current date and time: 2025-10-21 18:13:08.014703
Date and time after 30 days: 2025-11-20 18:13:08.014703
Date and time after 10 hours: 2025-10-22 04:13:08.014703
Date and time after 5 minutes: 2025-10-21 18:18:08.014703
Date and time after 1 second: 2025-10-21 18:13:09.014703

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です