Python string to datetime – Python: String to datetime or date object

String to datetime or date object in Python.

Python string to datetime: We will see in this article how to convert different format string to a DateTime or date object. We will do so using a function provided by Python’s date-time module to change string into a datetime object i.e. strptime( )

Syntax-

datetime.strptime(datetime_str, format):

Arguments:

  • datetime_str – It takes the date and time information
  • format – The format in which we want the date item information based on which the object will be created

The function will return the  date time object when the right parameters are provided. In case there is something wrong with the parameters provided to the function will throw a value error.

Convert string (‘DD/MM/YY HH:MM:SS ‘) to datetime object :

We can obtain the date time in our desired format by passing in the correct format code as arguments.

  • %d – Days
  • %m – Month
  • %y – Years
  • %H – Hours
  • %M – Minutes
  • &S – Seconds
  • %z – Timezone
  • %f – Milliseconds
from datetime import datetime
dateTimeString = '15/5/21 11:12:13'
# Converting the String ( ‘DD/MM/YY HH:MM:SS ‘) into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%d/%m/%y %H:%M:%S')
print(dateTimeObj)
Output :
2021-05-15 11:12:13

Convert string (‘MM/DD/YY HH:MM:SS ‘) to datetime object :

To achieve the ‘MM/DD/YY HH:MM:SS ‘ format we are goin to change the order of format from the previous code.

from datetime import datetime
dateTimeString = '5/15/2021 11:12:13'
#Converting the String ( ‘MM/DD/YY HH:MM:SS‘) into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%m/%d/%Y %H:%M:%S')
print(dateTimeObj)
Output :
2021-05-15 11:12:13

Convert string to datetime and handle ValueError :

In case we pass a format that is not compatible with the with the function, it throws a ValueError.

We can handle that error beforehand so that the program execution does not stops.

(For correct format)

from datetime import datetime
dateTimeString = '5/15/2021 11:12:13'
#Converting the String ( ‘MM/DD/YY HH:MM:SS‘) into a datetime object
try:
    dateTimeObj = datetime.strptime(dateTimeString, '%m/%d/%Y %H:%M:%S')
    print(dateTimeObj)
except ValueError as e:
    print(e)
Output :
2021-05-15 11:12:13

(For wrong format)

from datetime import datetime
dateTimeString = '5/15/2021 11:12:13'
#Converting the String ( ‘MM/DD/YY HH:MM:SS‘) into a datetime object
try:
    dateTimeObj = datetime.strptime(dateTimeString, '%d-%m-%Y %H:%M:%S')
    print(dateTimeObj)
except ValueError as e:
    print(e)
Output :
time data '5/15/2021 11:12:13' does not match format '%d-%m-%Y %H:%M:%S'

Python: Convert string to datetime – ( string format yyyy-mm-dd hh-mm-ss) :

from datetime import datetime
dateTimeString = '2021-5-15 11-12-13'
#Converting the String ( ‘yyyy-mm-dd hh-mm-ss‘) into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%Y-%m-%d %H-%M-%S')
print(dateTimeObj)
Output :
2021-05-15 11:12:13

Python: Convert string to datetime – ( string format MMM DD YYYY HH:MM:SS) :

from datetime import datetime
dateTimeString = 'May 15 2021 11:12:13'
#Converting the String ( ‘MMM DD YYYY HH:MM:SS‘) into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%b %d %Y %H:%M:%S')
print(dateTimeObj)
Output :
2021-05-15 11:12:13

Python: Convert string to datetime with milliseconds- ( string format DD/MM/YY HH:MM:SS:FFFFFF) :

In case we have the millisecond info,

from datetime import datetime
dateTimeString = '15/5/21 11:12:13.453'
#Converting the String ( ‘DD/MM/YY HH:MM:SS:FFFFFF‘) into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%d/%m/%y %H:%M:%S.%f')
print(dateTimeObj)
Output :
2021-05-15 11:12:13.453000

Python: Convert string to datetime with timezone :

To add the time zone in the format we have to include %z

from datetime import datetime
dateTimeString = '15/5/21 11:12:13+05:30'
#Converting the String wih timezone into a datetime object
dateTimeObj = datetime.strptime(dateTimeString, '%d/%m/%y %H:%M:%S%z')
print(dateTimeObj)
Output :
2021-05-15 11:12:13+05:30

Python: Convert string to date object :

To convert a datetime string into a date object we have to first convert it into a datetime object using strptime( ) and then pass it into the date( ) function to get our date object.

from datetime import datetime
dateTimeString = '2021-5-15'
#Converting the String into a date object
dateTimeObj = datetime.strptime(dateTimeString, '%Y-%m-%d').date()
print(dateTimeObj)
Output :
2021-05-15

Python: Convert string to time object :

To convert a datetime string into a time object we have to first convert it into a datetime object using strptime( ) and then pass it into the time( ) function to get our time object.

from datetime import datetime
dateTimeString = '11:12:13'
#Converting the String into a time object
dateTimeObj = datetime.strptime(dateTimeString, '%H:%M:%S').time()
print(dateTimeObj)
Output :
11:12:13