You will be much better off using the datetime.datetime.strptime() function, and then subtract the two results:
import datetime date1, time1 = '2012-09-08', '12:23:33' date2, time2 = '2012-09-08', '12:23:33' dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") print (dt1 - dt2).total_seconds()
Note that datetime.timedelta.seconds gives you the rest in seconds, there is also a .days attribute. Use .total_seconds() , it is much more convenient and saves your value to accept the value of .days .
Using the datetime.datetime.strptime() method, plus the timedelta methods, save most of the work in C code and should be faster.
Note that none of the methods is very fast due to the parsing step. Time Tests:
>>> import timeit >>> def parse_datetime(): ... date1, time1 = '2012-09-08', '12:23:33' ... date2, time2 = '2012-09-08', '12:23:33' ... dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") ... dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") ... (dt1 - dt2).total_seconds() ... >>> def parse_time(): ... d1 = time.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") ... d2 = time.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") ... seconds1 = time.mktime(d1) ... seconds2 = time.mktime(d2) ... seconds1 - seconds2 ... >>> timeit.timeit('test()', 'from __main__ import parse_datetime as test', number=10000) 0.6590030193328857 >>> timeit.timeit('test()', 'from __main__ import parse_time as test', number=10000) 0.7742340564727783
where the second method uses only the time.strptime() function (which outputs the time set directly).
There are no faster ways to parse the date-time syntax that I know of.