I need to return a string in the format "dd / mm / yyyy" in python, I got through the datetime library.
import datetime
date = datetime.datetime.now().strftime("%d/%m/%y")
But I also need the date 7 days ago, a week earlier.
I need to return a string in the format "dd / mm / yyyy" in python, I got through the datetime library.
import datetime
date = datetime.datetime.now().strftime("%d/%m/%y")
But I also need the date 7 days ago, a week earlier.
You can do the following, datetime.timedelta
:
import datetime
date_now = datetime.datetime.now()
seven_days_ago = date_now - datetime.timedelta(days=7)
print('now:', date_now.strftime("%d/%m/%y")) # 02/01/18
print('7 dias atrás:', seven_days_ago.strftime("%d/%m/%y")) # 26/12/17