Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i reach to hours in datetime

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading