Home > Mobile >  How to subtract 7 days from a given date?
How to subtract 7 days from a given date?

Time:05-03

How can I subtract 7 days from my entered date, April 22? I expected to get April 15.

from datetime import datetime, timedelta

date = str(input('Enter ATP date(Format Mmmmm Dd): '))
prev_date = date - timedelta(days=7)

error:

TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

CodePudding user response:

This should do the trick

from datetime import datetime, timedelta

date = str(input('Enter ATP date(Format Mmmmm Dd): '))
date = datetime.strptime(date, "%B %d")
prev_date = (date - timedelta(days=7)).strftime('%B %d')
  • Related