Python - 日付を計算する

日付の計算: timedelta()

日付を計算するには timedelta() 関数を使用します。

import datetime

dt_now = datetime.datetime.now()

print(dt_now)
# 2020-01-22 21:48:37.851694

print(now + datetime.timedelta(weeks=1))
# 2020-01-29 21:48:37.851694

print(now - datetime.timedelta(days=10))
# 2020-01-12 21:48:37.851694

print(now + datetime.timedelta(hours=10))
# 2020-01-23 07:48:37.851694

print(now - datetime.timedelta(minutes=10))
# 2020-01-22 21:38:37.851694

print(now + datetime.timedelta(seconds=10))
# 2020-01-22 21:48:47.851694

print(now + datetime.timedelta(days=7, hours=1, seconds=10))
# 2020-01-29 22:48:47.851694
日付の計算: timedelta()

関連記事