How to print n days back date at give time - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to print n days back date at give time (/thread-30158.html) |
How to print n days back date at give time - Mekala - Oct-10-2020 Hi, I want to print "N" days back date with specified time. I use below code but it error: from datetime import datetime, timedelta Quote:N = 2 error is below: TypeError Traceback (most recent call last) <ipython-input-3-5b8535c0ec9c> in <module>() 3 N = 2 4 ----> 5 date_N_days_ago = datetime.now().strftime("%Y-%M-%D 07:20:00") - timedelta(days=N) 6 7 print(datetime.now()) TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta' RE: How to print n days back date at give time - bowlofred - Oct-10-2020 strftime() turns it into a string. Do that only at the end, not before the math is done. from datetime import datetime, timedelta N = 2 date_N_days_ago = datetime.now() - timedelta(days=N) print(datetime.now()) print(date_N_days_ago) print(date_N_days_ago.strftime("%Y-%m-%d 07:20:00"))
|