문제

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