Question

I need to get how many seconds have passed since an element created which is stored as a datetime element in datastore (Google App Engine). I am using a datetime variable. But I could not convert the time difference to seconds. (Python 2.7)

Here is my aim:

def time_difference(datetime_time):
    return int(datetime.now()-datetime_time) #result should be in seconds like 612

Thanks a lot!

Was it helpful?

Solution

Subtracting datetime returns a timedelta. In Python 2.7 timedelta has a total_seconds() method.

def time_difference(datetime_time):
    delta = datetime.now() - datetime_time
    return int(delta.total_seconds())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top