Search code examples
pythondatetimestrptimestring-conversion

Python date time conversion


I have a string which would be like

"Wednesday, December 13, 2017 6:48:04 AM" 

which I need to convert it into "8/9/2017 10:44" format. I have tried using strptime, like so:

strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p') 

but it did not work.

Anyone has an idea how to do it?


Solution

  • Your format doesn't match your input. Try this instead:

    time.strptime(
        "Wednesday, December 13, 2017 6:48:04 AM",
        '%A, %B %d, %Y %I:%M:%S %p')
    

    Returns the proper

    time.struct_time(tm_year=2017, tm_mon=12, tm_mday=13,
                     tm_hour=6, tm_min=48, tm_sec=4,
                     tm_wday=2, tm_yday=347, tm_isdst=-1)