티스토리 뷰

공부

[Python] add date, week, month

seunggabi 승가비 2021. 1. 21. 23:12
728x90
import datetime
from dateutil.relativedelta import relativedelta


def string_to_date(dt, format='%Y%m%d'):
    return datetime.datetime.strptime(dt, format)

def date_to_string(date, format='%Y%m%d'):
    return datetime.datetime.strftime(date, format)

def add_date(dt, d):
    date = string_to_date(dt) + datetime.timedelta(days=d)
    return date_to_string(date)

def add_week(dt, d):
    date = string_to_date(dt) + datetime.timedelta(weeks=d)
    return date_to_string(date)

def add_month(dt, d):
    date = string_to_date(dt) + relativedelta(months=d)
    return date_to_string(date)

https://jaeyung1001.tistory.com/108

 

[Python] relativedelta함수 (timedelta엔 한달빼는게 왜없을까)

timedelta에서 month계산하는 방식이없어서 찾아보니 relativedelta 함수를 이용해서 계산한다고함 from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta target_date =..

jaeyung1001.tistory.com

 

728x90

'공부' 카테고리의 다른 글

[Python] concatenate images  (0) 2021.01.21
[License] MIT  (0) 2021.01.21
[Python] add date, week, month  (0) 2021.01.21
[Sh] command $? exit() & timer <= 30s failed  (0) 2021.01.21
[Sh] exit()  (0) 2021.01.21
[Python] weekday(); week of day (feat. date_to_string & string_to_date)  (0) 2021.01.21
댓글
댓글쓰기 폼