Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: difference of two timedate strings

I have two date strings (taken from user input and can vary greatly)

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

I wish to find the difference between the two as minutes.

How should I proceed to tackle this ?

like image 940
Simply Seth Avatar asked Sep 29 '11 18:09

Simply Seth


Video Answer


1 Answers

import datetime

d1 = datetime.datetime.strptime('2011:10:01:10:30:00', '%Y:%m:%d:%H:%M:%S')
d2 = datetime.datetime.strptime('2011:10:01:11:15:00', '%Y:%m:%d:%H:%M:%S')
diff = (d2 - d1).total_seconds() / 60

If you need to handle arbitrary datetime formats, I don't believe the built in datetime library will do that for you. Perhaps check out something like:

http://www.egenix.com/products/python/mxBase/mxDateTime/

like image 164
Mike Christensen Avatar answered Oct 06 '22 05:10

Mike Christensen