I'm trying to count days between two dates but I can't figure out how to do it.
This is the code I am using:
from datetime import datetime
from datetime import date
hoy = date(datetime.today().strftime("%Y,%m,%e")) #current time
otra_fecha = date(2022, 11, 5)
delta = hoy - otra_fecha
print(delta.days)
This is the error that is thrown:
TypeError: an integer is required (got type str)
CodePudding user response:
Use .date()
on datetime
object to get back the date
object:
hoy = datetime.today().date() #current time
CodePudding user response:
You're getting the TypeError since datetime.today().strftime("%Y,%m,%e")
returns a str and datetime.date() takes int as arguments.
- You can use date.today() to get the current local date.
hoy = date.today()
- You can also use the date() method to extract the date from datetime object.
hoy = datetime.today().date()