1

Given a string like this:

2020-08-14

How do I convert it to:

14 August 2020

Using python 3?

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
user3702643
  • 1,465
  • 5
  • 21
  • 48

3 Answers3

4

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'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

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'
RunTheGauntlet
  • 392
  • 1
  • 4
  • 15
-1
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)