banner

© 2004-20 ict4u.net

Formatting Date Time Strings

Python has a range of functions to ceate date and time object and format their appearance.

To be able to use them effectively, you will need to specify directive codes to tell python what the input format is and how you want the output to appear. Check out the list here for a full set of codes

from datetime import datetime
dates = []
dates.append('Jun 1 2005  1:33PM')
dates.append('Aug 28 1999 12:00AM')
dates.append('Dec 31 2015 11:00AM')

def reformat():
    # sample function to convert a list of dates into a preferred style for output
    for d in dates:
        date = datetime.strptime(d, '%b %d %Y %I:%M%p').strftime('%A, %d %B %Y')
        print type(date)
        print date

reformat()