Now()

Frank Millman frank at chagford.com
Thu Apr 26 01:32:43 EDT 2007


On Apr 25, 2:01 pm, Tim Golden <m... at timgolden.me.uk> wrote:
> Robert Rawlins - Think Blue wrote:
>
> > I'm using the following function 'str (now)' to place a date time stamp into
> > a log file, which works fine, however it writes the stamp like this.
> > 2007-04-25 11:06:53.873029
> > But I need to expel those extra decimal places as they're causing problems
> > with the application that parses the log data, all I need is something like
> > this:
> > 2007-04-25 11:06:53
> > With time depicted to the nearest second, from my background in ColdFusion
> > development we used to have a datetimeformat() function that I could use as
> > DateTimeFormat(now(), "yyyy-mm-dd HH:mm:ss")
> > Which would give the current time a mask.
> > Any equivalent for this in python?
>
> You want the strftime method which is documented under
> the time module.
>
> <trivial example>
> import datetime
> print datetime.datetime.now ().strftime ("%Y-%m-%d etc.")
>
> </trivial example>
>
> TJG

An alternative, which will work for this specific request -

str(now().replace(microsecond=0))

Frank Millman




More information about the Python-list mailing list