Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the difference between two times in python

Tags:

python

time

I am using python, and want to calculate the difference between two times.

Actually i had scenario to calculate the difference between login and logout times, for example in organizations there is some particular limit for working hours, so if a user login at 9:00 AM in the morning and if he logs out at 6:00 PM in the evening, we need to calculate how much duration he stayed in the office(which is 9 hours in the present scenario ), but i want to do this in python, so can anyone please let me know how to achieve the above concept of calculating the difference between login and logout times ?

like image 944
Fedrik Avatar asked Feb 25 '13 13:02

Fedrik


1 Answers

>>> start = datetime.datetime(year=2012, month=2, day=25, hour=9)
>>> end = datetime.datetime(year=2012, month=2, day=25, hour=18)
>>> diff = end - start
>>> diff
datetime.timedelta(0, 32400)
>>> diff.total_seconds()
32400
>>> diff.total_seconds() / 60 / 60
9
>>> 
like image 90
Fabian Avatar answered Oct 04 '22 11:10

Fabian