Search code examples
pythondatedatetimestrptimestrftime

Parse date string and change format


I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?


Solution

  • The datetime module could help you with that:

    datetime.datetime.strptime(input_date_string, input_format).strftime(output_format)
    

    For the specific example, you could do:

    >>> from datetime import datetime
    >>> datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
    '15/02/2010'
    

    Learn more about different formats here.