0
from datetime import datetime

today = datetime.today()
random_date = datetime(2022, 6, 19, 12, 37, 54)
time = today - random_date

print(time.hours)

How do I find how many hours have passed since random_date in this code?

1 Answers1

1

If I understood correctly, you may want to do this:

from datetime import datetime

today = datetime.today()
random_date = datetime(2022, 6, 19, 12, 37, 54)
time = today - random_date

# Use x2 slashes '//' in the division in order to get 
# an exact value 
print(time.total_seconds() / 3600)

Source: How do I convert datetime.timedelta to minutes, hours in Python?

Datetime docs: https://docs.python.org/3/library/datetime.html

Note: You can also use x2 slashes '//' in the division in order to round the number to floor.

Luka Cerrutti
  • 667
  • 1
  • 4
  • 9
  • 1
    For future reference, feel free to edit your original answer and undeleting it instead of posting a new answer. You may also want to include a version that uses `/` and explain the difference between `/` and `//`. – Code-Apprentice Aug 20 '22 at 00:41
  • 1
    @Code-Apprentice didn't know I can undelete answers. tysm ❤️ – Luka Cerrutti Aug 20 '22 at 00:49