Convert String to Datetime in Python

If you have a string and want to create a Datetime object out of it in Python, then you can use the Datetime Parse Time method, as follows:

from datetime import datetime your_datetime = datetime.strptime('May 31 2021 1:23PM', '%Y-%m-%d')
Code language: Python (python)

There are 2 very useful functions of datetime, namely:

  • strptime – Parse a string
  • strftime – Format a string

A common format you may be looking for is:

%Y-%m-%d %H:%M:%S

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments