How to get the date N days ago in Python
from datetime import datetime, timedelta
N = 2
date_N_days_ago = datetime.now() - timedelta(days=N)
print(datetime.now())
print(date_N_days_ago)
Results:
2019-03-01 13:02:15.056303
2019-02-27 13:02:15.056285
For more information see the timedelta documentation
Related posts
- Converting time zones for datetime objects in Python — posted 2009-05-05
- Python datetime / time conversions — posted 2008-11-12
- How to get the current date and time in Python — posted 2008-06-26
Comments
this may can help https://github.com/hustcc/timeago
disqus:2696072335
It works for me. Thank you. :)
disqus:3725370778