pythonの日付計算についてのメモ
日付の計算を行うにはdatedeltaを使う
import datetime today = datetime.datetime.now() print(today) date = today + datetime.timedelta(days=3) print(date)
結果
2018-12-04 10:51:09.709625 2018-12-07 10:51:09.709625
二つの日付の差を求める datetime型であれば加算・減算ができる
import datetime today = datetime.datetime.now() date = today + datetime.timedelta(days=3) sub = date - today print(sub) print((sub).days)
結果
3 days, 0:00:00 3