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!

有帮助吗?

解决方案

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())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top