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

房东的猫 提交于 2019-12-14 03:13:43

问题


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!


回答1:


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



回答2:


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'


来源:https://stackoverflow.com/questions/18797171/convert-an-integer-number-in-a-date-to-the-month-name-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!