2021-03-18

获取当日的前一个月和后一个月

import datetime
from dateutil.relativedelta import relativedelta

print(datetime.date.today())
print(datetime.date.today() - relativedelta(months=+1))
print(datetime.date.today() - relativedelta(months=-3)

 

获取上一个月第一天和最后一天

from datetime import date

from dateutil.relativedelta import relativedelta

today = date.today()

d = today - relativedelta(months=1) #这个1指上一个月

date(d.year,d.month,1) #这里获取上一个月的

date(today.year,today.month,1) - relativedelta(days=1) #这里获取上个月最后一天

上一篇:python获取日期(今日、昨日),及格式化日期的方法(datetime)


下一篇:别用 Date 了,Java8 新特性之日期处理,现在学会也不迟!