Thursday, February 29, 2024
HomeSoftware EngineeringConvert String to Datetime in Python

Convert String to Datetime in Python


In case you have a string and need to create a Datetime object out of it in Python, then you should use the Datetime Parse Time technique, as follows:

from datetime import datetime
your_datetime = datetime.strptime('Might 31 2021  1:23PM', '%Y-%m-%d')

There are 2 very helpful capabilities of datetime, specifically:

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

A typical format you could be searching for is:

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments