Domanda

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!

È stato utile?

Soluzione

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())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top