Frage

Hello I would like to know how i could convert the datetime object to a specific format.

>>> import datetime
>>> d = datetime.datetime.today()
>>> d.ctime()
'Sat Jan 26 17:33:08 2013'

I would like the output or to get the current date in the format below

2013-01-26 17:33:08
War es hilfreich?

Lösung

The isoformat() method gives you that:

d.isoformat(' ')

but might include microsends; you could use the .strftime() method for some more control:

d.strftime('%Y-%m-%d %H:%M:%S')

Output

>>> import datetime
>>> d = datetime.datetime.today()
>>> d.isoformat(' ')
'2013-01-26 13:12:08.628580'
>>> d.strftime('%Y-%m-%d %H:%M:%S')
'2013-01-26 13:12:08'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top