Skip to content Skip to sidebar Skip to footer

Spell Out Each Word Of A Date In Python 3

In Python 3, I would like to change 2018-01-01 to 'January first, two thousand and eighteen'. I looked at the format guide in the Python datetime documentation but I did not see a

Solution 1:

Using the inflect module, as commented by @ Lakshay Garg you can do:

import datetime as dt, inflect

p = inflect.engine()

def date_in_words(x):
     a = dt.datetime.strptime(x,'%Y-%m-%d')
     return (a.strftime('%B ')+p.number_to_words(p.ordinal(int(a.day)))+', ' + 
     p.number_to_words(int(a.year)))

date_in_words('2018-03-14')
Out[259]: 'March fourteenth, two thousand and eighteen'

Post a Comment for "Spell Out Each Word Of A Date In Python 3"