Site icon Machine Learning Projects

Python Program to Convert String to Datetime – 2023

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Convert String to Datetime.

For a detailed python, DateTime tutorial check out this blog – Python Program to get the current date and time

Example 1: Using the DateTime module

from datetime import datetime

my_date_string = "Oct 04 2022 01:33PM"

datetime_object = datetime.strptime(my_date_string, '%b %d %Y %I:%M%p')

print(type(datetime_object))
print(datetime_object)

Output

<class 'datetime.datetime'>
2022-10-04 13:33:00

Example 2: Using the dateutil module

from dateutil import parser

date_time = parser.parse("Oct 04 2022 01:33PM")

print(date_time)
print(type(date_time))

Output

2022-10-04 13:33:00
<class 'datetime.datetime'>

Check out our other python programming examples

Exit mobile version