0
import datetime

CurrentDate = datetime.date.today().strftime('%m/%d/%Y') 
print(CurrentDate)

UserInput =  input("When is your birthday? (mm/dd/yyyy) ")
birthday = datetime.datetime.strptime(UserInput, '%m/%d/%Y').date()
days = birthday - CurrentDate
print("your next birthday is in " + days )

It's giving me this error message:

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

I've tried to convert them with no result.

braaterAfrikaaner
  • 1,072
  • 10
  • 20
  • 3
    You're trying to subtract a `datetime.date` from a string. It sometimes helps to read the error messages ;-) – mechanical_meat Feb 01 '18 at 16:09
  • 1
    Possible duplicate to https://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python – Zack Tarr Feb 01 '18 at 16:13
  • `CurrentDate` is a string because you're calling `.strftime('%m/%d/%Y')` on it for whatever reason. – Aran-Fey Feb 01 '18 at 16:20
  • Possible duplicate of [How do I find the time difference between two datetime objects in python?](https://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python) – mechanical_meat Feb 01 '18 at 16:21

2 Answers2

0

your problem is type casting from date to str and vice versa:

import datetime

CurrentDate = datetime.date.today()
print(CurrentDate.strftime('%m/%d/%Y'))

UserInput =  input("When is your birthday? (mm/dd/yyyy) ")
birthday = datetime.datetime.strptime(UserInput, '%m/%d/%Y').date()
days = birthday - CurrentDate
print("your next birthday is in " + str(days))
mehrdadep
  • 972
  • 1
  • 15
  • 37
0

As bernie mentioned, CurrentDate is a string

import datetime

CurrentDate = datetime.date.today().strftime('%m/%d/%Y') 
print(CurrentDate)

UserInput =  input("When is your birthday? (mm/dd/yyyy) ")
birthday = datetime.datetime.strptime(UserInput, '%m/%d/%Y').date()
current_date = datetime.datetime.strptime(CurrentDate, '%m/%d/%Y').date()
print(birthday)

days = (birthday - current_date
print("your next birthday is in " + str(days) )

Your algorithm might still need some work :)

02/01/2018
When is your birthday? (mm/dd/yyyy) 01/01/2018
2018-01-01
your next birthday is in -31 days, 0:00:00

Dividing the tweaked time difference by days will look cleaner:

days = (birthday - current_date)/datetime.timedelta(days=1)
print("your next birthday is in " + str(days) + " days")
Anne
  • 583
  • 5
  • 15