0

Question How do I print out the remaining hours of overtime

Example: I have to be at work for 8 hours, and if my time goes over 8 hours as shown in OUTPUT then I just wanna have the 00:03:00 printed out.. Meaning that I have 3 min overtime that day.

from datetime import datetime

s1 = '07:15:00'
s2 = '16:18:00'
FMT = '%H:%M:%S'

tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)

print(tdelta) 

OUTPUT

9:03:00
peter
  • 53
  • 1
  • 1
  • 7
  • Take another look at your code. You probably assigned `s3 = '08:00:00` to reduce the difference of your `tdelta` but you never referenced it back. How did you expect it to work? A hint: look into `datetime.timedelta`, it's used to add/subtract time difference. – r.ook Oct 12 '18 at 13:10
  • Possible duplicate of [python time subtraction](https://stackoverflow.com/questions/13897246/python-time-subtraction) – r.ook Oct 12 '18 at 13:18

2 Answers2

0

You need to subtract the length of the working day (and it would appear an hour to account for breaks) from tdelta to determine the excess.

adambro
  • 310
  • 4
  • 16
0

Subtract the length of the working day with a timedelta object...

from datetime import datetime, timedelta

s1 = '07:15:00'
s2 = '16:18:00'
FMT = '%H:%M:%S'

work_time = timedelta(hours=8)
lunch_time = timedelta(hours=1)

tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT) - work_time - lunch_time

print(tdelta) 

Output:

0:03:00
Martin Stone
  • 12,682
  • 2
  • 39
  • 53