Sleep calculator with decimal in python

Question:

I’m looking for a way to calculate my time slept by using a formula with decimal numbers ?

For exemple :

If I go to bed at 23h55 I will write : 23.9

And I get up at 7h05 I will write : 7.1

How do I calculate the difference between them as time slept ?

Can anyone help me ?

Asked By: Moaz

||

Answers:

Python has the handy datetime module to facilitate this sort of thing. You can express times as a datetime object and then simply calculate the difference between them:

import datetime

to_bed = datetime.datetime(year=2022, month=8, day=17, hour=23, minute=55)
got_up = datetime.datetime(year=2022, month=8, day=18, hour=7, minute=5)

time_slept = got_up - to_bed
print(time_slept)

Output: 7:10:00, indicating the interval between the two.

Answered By: Schnitte
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.