Thomas Nicholls
Thomas Nicholls

Reputation: 21

How can I multiply an hourly wage by a python datetime time delta

I am making a program that allows user to track how long shifts are at a work place, I have this code:

import time
import datetime as dt

start = str(dt.datetime.now().strftime('%H:%M:%S'))
print(start)

input()

end = str(dt.datetime.now().strftime('%H:%M:%S'))
print(end)

form = '%H:%M:%S'
tdelta = dt.datetime.strptime(end,form) - dt.datetime.strptime(start,form)

pay = 10

print(tdelta)
print(tdelta * pay)

this is not the code for my actual program this is just an example I have written to test the functionality, input() is used to pause program until enter is pressed so that the duration can be calculated. I need the desired program to multiply 'pay' by duration, which is 'tdelta'. for examply if pay is 10 and the duration is 2 hours i want the output to be 20 or £20.

Cheers!

Upvotes: 0

Views: 467

Answers (1)

Poxman
Poxman

Reputation: 11

You could change

tdelta = dt.datetime.strptime(end,form) - dt.datetime.strptime(start,form)

to

tdelta = (dt.datetime.strptime(end,form) - dt.datetime.strptime(start,form)).seconds / 3600

to get tdelta in exact number of hours

Upvotes: 1

Related Questions