python 'x days ago' to datetime

前端 未结 7 2092
谎友^
谎友^ 2021-01-11 09:35

I have strings that show a date in the following format:

x minutes/hours/days/months/years ago

I need to parse that to a datetime using pyt

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-11 09:55

    Easiest way is to use dateparser:

    import dateparser
    date_ago="4 months ago" 
    date=dateparser.parse(date).strftime("%Y-%m-%d")
    date
    

    Output:

    '2020-01-08'
    

提交回复
热议问题