LoginSignup
2
0

More than 5 years have passed since last update.

python 年月日を計算

Last updated at Posted at 2019-03-20

年月日を計算

  • relativedelta
import datetime
from dateutil.relativedelta import relativedelta

dt = datetime.datetime(2017,1,1)
print(dt, '(基準年月日)')

# 3年後
dt_plus_year = dt + relativedelta(years=3, months=6, days=23)
print(dt_plus_year, '(3年6ヶ月23日後)')

# 1ヶ月前
dt_minus_month = dt + relativedelta(months=-1)
print(dt_minus_month, '(1ヶ月前)')

# 90日前
dt_minus_day = dt + relativedelta(days=-90)
print(dt_minus_day, '(90日前)', type(dt_minus_day))
[out]

2017-01-01 00:00:00 (基準年月日)
2020-07-24 00:00:00 (3年6ヶ月23日後)
2016-12-01 00:00:00 (1ヶ月前)
2016-10-03 00:00:00 (90日前) <class 'datetime.datetime'>
2
0
1

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
2
0