-
[Python] dateutil 모듈을 이용한 전년,전월,전일,D-day 날짜 구하기Python/Python Programming 2019. 10. 21. 18:50
파이썬 dateutil 모듈을 활용한 전년,전월,전일, D-day 구하기
2019-10-21 기준
전년:동월:동일
from datetime import datetime from dateutil.relativedelta import * now = datetime.now().date() print(now+relativedelta(years=-1)) ------------------------------------- 2018-10-21
동년:전월:동일
from datetime import datetime from dateutil.relativedelta import * now = datetime.now().date() print(now+relativedelta(months=-1)) -------------------------------------- 2019-09-21
동년:전월:전일
from datetime import datetime from dateutil.relativedelta import * now = datetime.now().date() print(now+relativedelta(days=-1)) -------------------------------------- 2019-10-20
특정일로 부터 얼마나 시간이 흘렀는지
from datetime import datetime from dateutil.relativedelta import * now = datetime.now() year, month, day = 1981, 1, 1 day = relativedelta(now, datetime(year, month, day)) print('%s years, %s months, %s days' %(day.years, day.months, day.days)) ------------------------------------------------------------------------- 38 years, 9 months, 20 days
특정일로 부터 얼마나 시간이 남았는지 (D-Day 계산기)
from datetime import datetime from dateutil.relativedelta import * now = datetime.now() year, month, day = 2019, 11, 1 day = relativedelta(now, datetime(year, month, day)) print('%s years, %s months, %s days' %(day.years, day.months, day.days)) ------------------------------------------------------------------------ 0 years, 0 months, -10 days
'Python > Python Programming' 카테고리의 다른 글