Python Forum
How to subtract the DayOfWeek from date from a integer? - 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 subtract the DayOfWeek from date from a integer? (/thread-27151.html)



How to subtract the DayOfWeek from date from a integer? - DarkCoder2020 - May-27-2020

I'm trying to subtract a date such as 05/01/2020 from a integer such as -1. Any suggestions?

int(cnt)

            dteStartDate =datetime.strptime(dteStartDate,'%m/%d/%Y').date()
            if dteStartDate.weekday() != 1 :
                cnt = cnt+1
                dteStartDate = m0weekbeg - cnt
Error is below:
File "C:\Python37-32\pr_import.py", line 251, in proceed
dteStartDate = m0weekbeg - cnt
TypeError: unsupported operand type(s) for -: 'str' and 'int'


RE: How to subtract the DayOfWeek from date from a integer? - scidam - May-27-2020

Use datetime.timedelta instead. Something like the following:

dteStartDate=m0weekbeg - datetime.timedelta(days=cnt)



RE: How to subtract the DayOfWeek from date from a integer? - bowlofred - May-27-2020

Are you looking to change the date by an integral number of days? Then create a timedelta object and add or subtract that.


>>> datetime.datetime.now().date()
datetime.date(2020, 5, 27)
>>> datetime.datetime.now().date() + datetime.timedelta(4)
datetime.date(2020, 5, 31)
>>> datetime.datetime.now().date() + datetime.timedelta(-873)
datetime.date(2018, 1, 5)