Python获取指定日期前后n月的时间

  • Post author:
  • Post category:python


>>> from datetime import datetime, timedelta
>>> from dateutil.relativedelta import relativedelta
>>>
>>> d_str = '20200612'
>>> diff = 1
>>> d = str(datetime.date(datetime.strptime(d_str, '%Y%m%d')) - relativedelta(months=+diff)).replace("-", '')
>>> d
'20200512'
>>>

# 输入输出的时间格式根据需要的格式调整



版权声明:本文为lquarius原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。