Datetime.utcnow () time format

I saved the time as presentTime=datetime.datetime.utcnow()

Exit: 2014-08-18 21:11: 35.537000. How can this be formatted before: August 18, 2014 - 21:11:35 instead?

+5
source share
3 answers

datetime.utcnow returns a datetime object, you can use the strftime method to convert it to a string of the required format:

 >>> datetime.datetime.utcnow().strftime('%B %d %Y - %H:%M:%S') 'August 20 2014 - 13:55:49' 
+10
source

the object you receive is an instance of datetime . Just format it using strftime() method: https://docs.python.org/2.7/library/datetime.html?highlight=date#datetime.datetime.strftime

update (thanks @Ffisegyedd):

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

+2
source
 import datetime presentTime=datetime.datetime.utcnow() print presentTime.strftime('%B %d %Y - %H:%M:%S') 
+1
source

Source: https://habr.com/ru/post/1200756/


All Articles