216
t = e['updated_parsed']
dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6]
print dt
>>>2010-01-28 08:39:49.000003

How do I turn that into a string?:

"January 28, 2010"
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

8 Answers8

291

The datetime class has a method strftime. The Python docs documents the different formats it accepts:

For this specific example, it would look something like:

my_datetime.strftime("%B %d, %Y")
RNHTTR
  • 2,235
  • 2
  • 15
  • 30
Cristian
  • 42,563
  • 25
  • 88
  • 99
123

Here is how you can accomplish the same using python's general formatting function...

>>>from datetime import datetime
>>>"{:%B %d, %Y}".format(datetime.now())

The formatting characters used here are the same as those used by strftime. Don't miss the leading : in the format specifier.

Using format() instead of strftime() in most cases can make the code more readable, easier to write and consistent with the way formatted output is generated...

>>>"{} today's date is: {:%B %d, %Y}".format("Andre", datetime.now())

Compare the above with the following strftime() alternative...

>>>"{} today's date is {}".format("Andre", datetime.now().strftime("%B %d, %Y"))

Moreover, the following is not going to work...

>>>datetime.now().strftime("%s %B %d, %Y" % "Andre")
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    datetime.now().strftime("%s %B %d, %Y" % "Andre")
TypeError: not enough arguments for format string

And so on...

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • 2
    I've been wondering what are the benefits to using format() for this case, over strftime(). Does either method present a benefit other than personal preference? What are the performance differences? – Andre May 29 '14 at 12:17
  • 2
    @Andre Thanks for your question! Personal preferences are often guided by experience. Please see my updated answer for why I prefer format() over strftime(). Performance and Python are two words which do not go well together. I wouldn't be overly concerned with performance if I have decided to use Python. When I need performance I use some statically typed high performance language. – Sandeep Datta May 29 '14 at 17:53
  • 2
    The format method is simpler in most cases. Compare: `print 'Today is {:%B %d, %Y}'.format(datetime.now())` to `print 'Today is {}'.format(datetime.now().strftime('%B %d, %Y')`. When you are using the format() method anyways, why not use it for the date formatting too? – ChaimG Aug 30 '16 at 21:58
77

Using f-strings, in Python 3.6+.

from datetime import datetime

date_string = f'{datetime.now():%Y-%m-%d %H:%M:%S%z}'
Natim
  • 17,274
  • 23
  • 92
  • 150
35

very old question, i know. but with the new f-strings (starting from python 3.6) there are fresh options. so here for completeness:

from datetime import datetime

dt = datetime.now()

# str.format
strg = '{:%B %d, %Y}'.format(dt)
print(strg)  # July 22, 2017

# datetime.strftime
strg = dt.strftime('%B %d, %Y')
print(strg)  # July 22, 2017

# f-strings in python >= 3.6
strg = f'{dt:%B %d, %Y}'
print(strg)  # July 22, 2017

strftime() and strptime() Behavior explains what the format specifiers mean.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
16

Python datetime object has a method attribute, which prints in readable format.

>>> a = datetime.now()
>>> a.ctime()
'Mon May 21 18:35:18 2018'
>>> 
SuperNova
  • 25,512
  • 7
  • 93
  • 64
10

Read strfrtime from the official docs.

Nathan Smith
  • 683
  • 1
  • 10
  • 24
S.Lott
  • 384,516
  • 81
  • 508
  • 779
2

For those who are impatient to read the nice official docs strftime :)

from datetime import datetime
datetime.now().strftime("%H:%M %B %d, %Y")
'12:11 August 08, 2022'
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
-5

This is for format the date?

def format_date(day, month, year):
        # {} betekent 'plaats hier stringvoorstelling van volgend argument'
        return "{}/{}/{}".format(day, month, year)