문제

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
도움이 되었습니까?

해결책

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'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top