views:

96

answers:

3

How to calculate the difference in time in minutes for the following timestamp in python

  2010-01-01 17:31:22
 2010-01-03 17:31:22
+5  A: 

Use datetime.strptime() to parse into datetime instances, and then compute the difference, and finally convert the difference into minutes.

unwind
+2  A: 
from datetime import datetime

fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)

print (d2-d1).days * 24 * 60
RSabet
A: 

As was kind of said already, you need to use datetime.datetime's strptime method:

from datetime import datetime

fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)

daysDiff = (d2-d1).days

# convert days to minutes
minutesDiff = daysDiff * 24 * 60

print minutesDiff
Pilgrim