mongotop
mongotop

Reputation: 5774

convert an integer number in a date to the month name using python

I do have this string '2013-07-01' and I want to convert it to July 1, 2013
How to do that using python?

Thanks!

Upvotes: 0

Views: 5186

Answers (2)

TerryA
TerryA

Reputation: 59974

Using the datetime module:

>>> import datetime
>>> s = '2013-07-01'
>>> mydate = datetime.datetime.strptime(s, '%Y-%m-%d')
>>> print mydate.strftime('%B %d, %Y')
July 01, 2013

Upvotes: 7

Developer
Developer

Reputation: 8400

To add Haidro's answer, you could do it in just one compact line:

>>> s = '2013-07-01'
>>> print datetime.datetime.strptime(s,'%Y-%m-%d').strftime('%B %d, %Y')
'July 01, 2013'

Upvotes: 1

Related Questions