Search code examples
pythondatetimepython-datetime

Python: How to convert date string into datetime object


I have a date string - "Fri Jun 05 15:59:14 PDT 2020" How to convert into datetime object?

i have tried below format, its not working.

'%a %b %d %H:%M:%S %Z %Y'

Solution

  • from dateutil import parser
    
    date_string = "Fri Jun 05 15:59:14 PDT 2020"
    datetime_obj = parser.parse(date_string)
    
    print(datetime_obj)
    

    output:-

    2020-06-05 15:59:14
    

    try using parse to Converts the string representation of a date and time to its DateTime equivalent,