Python获取当前时间的上一年,上一月,上一日

使用python获取当前月份之前的一个月

    datetimeNow = datetime.date.today()
    print(datetimeNow)
    datetimeOneMonthAgo = str(datetimeNow - relativedelta(months=1))
    print(datetimeOneMonthAgo)
    datetimeOneYearAgo = datetimeNow - relativedelta(years=1)
    print(datetimeOneYearAgo)
    datetimeOneDayAgo = datetimeNow - relativedelta(days=1)
    print(datetimeOneDayAgo)

2022-05-06
2022-04-06
2021-05-06
2022-05-05

你可能感兴趣的:(python学习,python)