主要使用的是dateutil
工具包(python自带)的relativedelta.py
文件下的relativedelta
类
导入时使用:
from dateutil.relativedelta import relativedelta
代码示例
配合pandas的日期使用
import pandas as pd
from dateutil.relativedelta import relativedelta
this_date = pd.to_datetime("2020-01-01")
print(this_date, " ", this_date + relativedelta(months=1, days=1))
得到结果:
2020-01-01 00:00:00 2020-02-02 00:00:00
配合datetime的日期使用:
from dateutil.relativedelta import relativedelta
from datetime import datetime
this_date = datetime(2020, 1, 1)
print(this_date, " ", this_date + relativedelta(months=1, days=1))
得到同样的结果:
2020-01-01 00:00:00 2020-02-02 00:00:00