Given a string like this:
2020-08-14
How do I convert it to:
14 August 2020
Using python 3?
Given a string like this:
2020-08-14
How do I convert it to:
14 August 2020
Using python 3?
You can use the datetime
module for reformatting date strings. Using strptime
you can read from a string into a datetime
object, then using strftime
you can convert back to a string with a different format.
>>> from datetime import datetime
>>> d = datetime.strptime('2020-08-14', '%Y-%m-%d')
>>> d
datetime.datetime(2020, 8, 14, 0, 0)
>>> d.strftime('%d %B %Y')
'14 August 2020'
An alternative approach using pandas function below:
import pandas as pd
d = pd.to_datetime('2020-08-14')
d.strftime('%d %B %Y')
Out[11]: '14 August 2020'
monthDict = {
'01' : 'January',
'02' : 'February',
'03' : 'March',
'04' : 'April',
'05' : 'May',
'06' : 'June',
'07' : 'July',
'08' : 'August',
'09' : 'September',
'10' : 'October',
'11' : 'November',
'12' : 'December'
}
readDate = input('Pleaes enter the date in the yyyy-mm-dd format: ')
day = readDate.split('-')[2]
month = readDate.split('-')[1]
year = readDate.split('-')[0]
print('Printing in the dd month year format: ', day, monthDict[month], year)