Convert String to Datetime in Python

0 min read 69 words

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')

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:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags