i have raw date = March 5, 2024, 5:33 a.m.
is it possible to convert it in datetime.datetime(2024, 3, 5, 5, 33) using python
I have tried
from datetime import datetime
date_string = "March 5, 2024, 5:33 a.m."
date_object = datetime.strptime(date_string, "%B %d, %Y, %I:%M %p")
desired_datetime = datetime(2024, 3, 5, 5, 33)
print(desired_datetime)
Your solution is almost correct. The issue in your code is in the format specifier (
%I:%M %p). The%Ispecifier is used for hours in 12-hour format, but your input string has only a single digit for the hour, which means you should use%Iinstead of%Iin your format string. This is the corrected version of your code.