[Tutor] Formating from hhmms to hh:mm:ss

Kent Johnson kent37 at tds.net
Mon Sep 8 03:57:36 CEST 2008


On Sun, Sep 7, 2008 at 9:33 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> Here's as far as I can go with this. The last line of output asks the
> question I need an answer for.
> (Once again my direct post to tutor at python.org has failed to appear here
> (sent 5:42 PDT), as before.)

It arrived late for some reason.

You can do what you want much more simply using datetime.strptime()
and strftime():
In [2]: from datetime import datetime, timedelta
In [3]: format = '%Y%m%d_%H%M%S'
In [8]: d=datetime.strptime('20080321_113405', format)
In [10]: d
Out[10]: datetime.datetime(2008, 3, 21, 11, 34, 5)
In [13]: d += timedelta(seconds=200)
In [14]: d
Out[14]: datetime.datetime(2008, 3, 21, 11, 37, 25)
In [15]: d.strftime(format)
Out[15]: '20080321_113725'

Kent


More information about the Tutor mailing list