What's the simplest way to subtract a month from a date in Python?

前端 未结 21 1973
[愿得一人]
[愿得一人] 2020-12-02 09:12

If only timedelta had a month argument in it\'s constructor. So what\'s the simplest way to do this?

EDIT: I wasn\'t thinking too hard about this as was poin

21条回答
  •  广开言路
    2020-12-02 09:44

    import datetime
    date_str = '08/01/2018'
    format_str = '%d/%m/%Y'
    datetime_obj = datetime.datetime.strptime(date_str, format_str)   
    datetime_obj.replace(month=datetime_obj.month-1)
    

    Simple solution, no need for special libraries.

提交回复
热议问题