w3resource

Python: Convert a string into datetime

Python Datetime: Exercise-49 with Solution

Write a Python program to convert a string into datetime.

Sample Solution:

Python Code:

from datetime import datetime

date_obj = datetime.strptime('May 12 2016  2:25AM', '%b %d %Y %I:%M%p')
print()
print(date_obj)
print()

Sample Output:

2016-05-12 02:25:00 

Flowchart:

Flowchart: Convert a string into datetime.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to display a simple, formatted calendar of a given year and month.
Next: Write a Python program to get a list of dates between two dates.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()