First you'll need to convert the string to a Python datetime.datetime
object to easily work with it. To do that you can use classmethod datetime.strptime(date_string, format)
(see in docs), which returns a datetime corresponding to date_string, parsed according to format.
Then, to print the datetime object as any string you'd want, there is this other method datetime.strftime(format)
(see in docs) which return a string representing the date and time, controlled by an explicit format string.
(Note: For more about the formating directives, follow this link to docs)
So for the given string you could proceed as follow:
from datetime import datetime
def get_suffix(day: int) -> str:
# https://stackoverflow.com/a/739266/7771926
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
return suffix
def process_date(date: str) -> str:
dt_obj = datetime.strptime(date, '%Y%m%dT%H%M%S')
return dt_obj.strftime('%I%p, %d{} %B %Y').format(get_suffix(dt_obj.day))
def main():
date_str = '20131019T150000'
print(process_date(date_str))
if __name__ == '__main__':
main()
If you execute the script, this is what is printed to console: 03PM, 19th October 2013
Glad if helps.