Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format datetime.utcnow() time

I have saved a time as presentTime=datetime.datetime.utcnow()

It 's output is 2014-08-18 21:11:35.537000. How can this be formatted to : August 18 2014 - 21:11:35 instead?

like image 753
user94628 Avatar asked Aug 20 '14 13:08

user94628


2 Answers

datetime.utcnow returns a datetime object. You can use its strftime method to convert it into a string of your desired format:

>>> datetime.datetime.utcnow().strftime('%B %d %Y - %H:%M:%S')
'August 20 2014 - 13:55:49'
like image 104
poke Avatar answered Oct 20 '22 20:10

poke


the object you're getting is a datetime instance. Just format it via its method strftime(): https://docs.python.org/2.7/library/datetime.html?highlight=date#datetime.datetime.strftime

update (thx @Ffisegyedd):

possible placeholders values: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

like image 44
yedpodtrzitko Avatar answered Oct 20 '22 21:10

yedpodtrzitko