Q:

Datetime differences on python

I have  DateTime, from string I convert them on IS0 format example. ISODate("2016-06-24T09:07:31.097Z") 
I want to find the differences between them using python, so I did this:
string_older = "2016-05-18T20:53:43.776456"
string_young = "2016-05-16T20:53:43.776456"
datetime_older = datetime.datetime.strptime(string_older, "%Y-%m-%dT%H:%M:%S.%f") //date on ISO format
datetime_young = datetime.datetime.strptime(string_young, "%Y-%m-%dT%H:%M:%S.%f") //date on ISO format
a = time.mktime(datetime_older)
b = time.mktime(datetime_young)
diff = a - b
seconds = int(diff) % 60

But this gives this error TypeError: argument must be 9-item sequence, not datetime.datetime 
at this line time.mktime(datetime_older).
I don't know how to fix it? Please help.

A:

Subtracting both datetimes gives you a timedelta. To get the difference expressed in seconds, call its total_seconds method.
string_older = "2016-05-18T20:53:43.776456"
string_young = "2016-05-16T20:53:43.776456"
datetime_older = datetime.datetime.strptime(string_older, "%Y-%m-%dT%H:%M:%S.%f") //date on ISO format
datetime_young = datetime.datetime.strptime(string_young, "%Y-%m-%dT%H:%M:%S.%f")

timedelta = datetime_older - datetime_young
seconds = timedelta.total_seconds()

timedelta.total_seconds()
Return the total number of seconds contained in the duration. Equivalent to (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 computed with true division enabled.

