-2

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)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
paquinho
  • 1
  • 1

2 Answers2

1

You're getting the TypeError since datetime.today().strftime("%Y,%m,%e") returns a str and datetime.date() takes int as arguments.

  1. You can use date.today() to get the current local date.
hoy = date.today()
  1. You can also use the date() method to extract the date from datetime object.
hoy = datetime.today().date()
sagamantus
  • 71
  • 1
  • 4
0

Use .date() on datetime object to get back the date object:

hoy = datetime.today().date() #current time
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26