본문 바로가기

Python/date & time

날짜 구하기 # 한달 전의 날짜 구하기

728x90
반응형

1) datetime, timedelta 

from datetime import datetime, timedelta

today = datetime.now()
one_month_earlier = today - timedelta(days=30)
print(one_month_earlier)

결과값: 2022-11-30 22:56:51.045597

 

 

2) datetime, relativedelta(from dateutil)

from datetime import datetime
# pip install python-dateutil
from dateutil.relativedelta import relativedelta

today = datetime.now()
one_month_earlier = today + relativedelta(months=-1)	# years, days
print(one_month_earlier)

결과값: 2022-11-30 22:56:51.045597

728x90
반응형