[Tutor] comparing dates
Chris Calloway
cbc at unc.edu
Sat Nov 24 06:58:39 CET 2007
On Nov 24, 2007, at 12:27 AM, Lawrence Shafer wrote:
> How would I compare these two dates and extract the difference in
> H:M:S??
http://docs.python.org/lib/module-datetime.html
http://docs.python.org/lib/datetime-timedelta.html
> 22 Nov 2007 18:54:07
> 23 Nov 2007 23:24:23
>>> import datetime
>>> a = datetime.datetime(2007,11,22,18,54,7)
>>> b = datetime.datetime(2007,11,23,23,24,23)
>>> c = b - a
>>> hours = (c.seconds / (60*60))
>>> minutes = (c.seconds - (hours * 60*60)) / 60
>>> seconds = c.seconds - (hours * 60*60) - (minutes * 60)
>>> print str((c.days*24) + hours) + ":" + str(minutes) + ":" + str
(seconds)
28:30:16
>>>
--
Sincerely,
Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall cell: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071124/52c29015/attachment.htm
More information about the Tutor
mailing list