Question

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
Was it helpful?

Solution

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top