Reputation: 11
Views Let us Consider shift.start_time : 14:30 shift.end_time : 15:30 of same day. Actual time difference is 1 hours.But its showing 22 hours.But I need to get 1 hr.
for shift in shift_bokings:
if shift.start_time < shift.end_time:
start = pendulum.instance(timezone.now()).start_of('day')
end = start.end_of('day')
print("SHIFT START AND END TIME",shift.start_time,shift.end_time)
dateTime3 = datetime.datetime.combine(datetime.date.today(), shift.start_time) - datetime.datetime.combine(datetime.date.today(), shift.end_time)
# dateTimeDifference = (dateTime3.total_seconds() /3600 )
# y = dateTimeDifference - shift.break_time
print("DATETIME IS",dateTime3,end)
else:
dateTime3 = datetime.datetime.combine(datetime.date.today(), shift.end_time) - datetime.datetime.combine(datetime.date.today(), shift.start_time)
print("DATETIME 3 IS",dateTime3)
dateTimeDifferenceInHours = (dateTime3.seconds / 3600)
work_time = dateTimeDifferenceInHours - shift.break_time
print('WORK TIME',work_time)
writer.writerow([shift.date, shift.worker_name, shift.shift_status, shift.location, shift.start_time, shift.end_time, shift.break_time, work_time])
return response
Upvotes: 0
Views: 335
Reputation: 14644
Assuming your start and end times are 14:30 and 15:30 of the same day, then you just subtract one from the other to get the difference of 1 hour:
from datetime import datetime, timezone
start_time = datetime.strptime("2020-05-20 14:30:00", "%Y-%m-%d %H:%M:%S").astimezone(timezone.utc)
end_time = datetime.strptime("2020-05-20 15:30:00", "%Y-%m-%d %H:%M:%S").astimezone(timezone.utc)
print(start_time, end_time)
print( (end_time - start_time).seconds / 3600 )
Outputs:
2020-05-20 14:30:00+00:00 2020-05-20 15:30:00+00:00
1.0
I'm not sure what all the other stuff in your code - pendulum
and the start and end of day parts - has to do with getting the difference between two times, which is your question.
Upvotes: 1