0

Im trying to subtract a date string in python but get this error trap.

if intDay >= 28:
    m0weekbeg = str(wendMonth) + "/16/" + str(wendYear)
    weekBegDay = "16"
    weekEndDay = str(intDay)

dteStartDate = m0weekbeg
mstartdate = m0weekbeg 
mstopdate = m0weekend
cnt= 0
int(cnt)
dteStartDate =datetime.strptime(dteStartDate,'%m/%d/%Y')
startDay = str(dteStartDate.day)
while startDay != 2:
    cnt = cnt+1
    dteStartDate = m0weekbeg-cnt

Error:

  File "C:\Python37-32\pr_import.py", line 242, in proceed
    dteStartDate = m0weekbeg-cnt
TypeError: unsupported operand type(s) for -: 'str' and 'int'
wjandrea
  • 16,334
  • 5
  • 30
  • 53

1 Answers1

0

m0weekbeg is a string and you can't subtract int from str:

if intDay >= 28:
    m0weekbeg = datetime.date(wendYear, wendMonth, 16)#<<<<<<<<<<<<<<<
    weekBegDay = "16"
    weekEndDay = str(intDay)

dteStartDate = m0weekbeg.strftime('%m/%d/%Y')
mstartdate = m0weekbeg.strftime('%m/%d/%Y')
mstopdate = m0weekend
cnt= 0
int(cnt)
dteStartDate =datetime.strptime(dteStartDate,'%m/%d/%Y')
startDay = str(dteStartDate.day)
while startDay != 2:
    cnt = cnt+1
    dteStartDate = m0weekbeg-datetime.timedelta(days=cnt)#<<<<<<<<<<<<<<

but this code looks really awful. I think it would be a better idea to rewrite it instead of just translating it statement for statement.

bb1950328
  • 1,053
  • 8
  • 14