Write a Python program to convert a string to datetim


The program is using the datetime module to parse a date string and create a datetime object. In this case, the string "JUNE 1 2023 5:30 PM" is being parsed with the strptime() method, which takes two arguments: the string to be parsed and a string indicating the format of the string.

The format string "%B %d %Y %I:%M %p" specifies that the date string has the month name (e.g. "June"), the day of the month (e.g. "1"), the year (e.g. "2023"), the hour in 12-hour format (e.g. "5"), the minutes (e.g. "30"), and the AM/PM indicator (e.g. "PM"). The resulting datetime object is then printed to the console using the print() function.

Source Code

from datetime import datetime
dt = datetime.strptime("JUNE 1 2023 5:30 PM", "%B %d %Y %I:%M %p")
print(dt)

Output

2023-06-01 17:30:00

Example Programs