Date&Time

Convert string to datetime object

from datetime import datetime

def str_to_datetime(str,strp = "%d/%m/%Y"):
    datetime_object = datetime.strptime(str, strp)
    
    return datetime_object

Example

str = '06/10/2015'

dt = str_to_datetime(str)
print(dt)

# 2015-10-06 00:00:00
str = 'Jun 10 2015 4:31:29PM'
strp = '%b %d %Y %I:%M:%S%p'

dt = str_to_datetime(str,strp)
print(dt)

# 2015-06-10 16:31:29

Last updated