In this article we will discuss how to convert timestamp in different string formats to a datetime class object in Python.

String to datetime object using datetime.strptime()

Python’s datetime module provides a datetime class, which has a method to convert string to a datetime object i.e.

datetime.strptime(date_string, format)

If accepts a string containing the timestamp and a format string containing the format codes representing the date time elements in date_string. It parses the string according to format codes and returns a datetime object created from it.

To use this import datetime class from datetime module i.e.

from datetime import datetime

Let’s see some examples

Example 1:

Let’s convert a time string in format YYYY-MM-DDTHH::MM::SS.MICROS to a datetime object i.e.

datetimeObj = datetime.strptime('2018-09-11T15::11::45.456777', '%Y-%m-%dT%H::%M::%S.%f')

print(datetimeObj)

print(type(datetimeObj))

Output:
2018-09-11 15:11:45.456777

<class 'datetime.datetime'>

Format string used here is : ‘%Y-%m-%dT%H::%M::%S.%f

Complete list of format code :

Format Codes Description Example
%d Day of the month as a zero-padded decimal number 01, 02, 03, 04 …, 31
%a Weekday as abbreviated name Sun, Mon, …, Sat
%A Weekday as full name Sunday, Monday, …, Saturday
%m Month as a zero-padded decimal number 01, 02, 03, 04 …, 12
%b Month as abbreviated name Jan, Feb, …, Dec
%B Month as full name January, February, …, December
%y Year without century as a zero-padded decimal number 00, 01, …, 99
%Y Year with century as a decimal number 0001, …, 2018, …, 9999
%H Hour (24-hour clock) as a zero-padded decimal number 01, 02, 03, 04 …, 23
%M Minute as a zero-padded decimal number 01, 02, 03, 04 …, 59
%S Second as a zero-padded decimal number 01, 02, 03, 04 …, 59
%f Microsecond as a decimal number, zero-padded on the left 000000, 000001, …, 999999
%I Hour (12-hour clock) as a zero-padded decimal number 01, 02, 03, 04 …, 12
%p Locale’s equivalent of either AM or PM AM , PM
%j Day of the year as a zero-padded decimal number 01, 02, 03, 04 …, 366

Let’s see some other examples :

Example 2:

Let’s convert a timestamp string in format DD/MM/YYYY HH::MM::SS to a datetime object i.e.

datetimeObj = datetime.strptime('23/Jan/2018 14:12:22', '%d/%b/%Y %H:%M:%S')

print(datetimeObj)

Output:
2018-01-23 14:12:22

Example 3:

Create Date Time Object from date string only in ‘DD MMM YYYY‘ format

datetimeObj = datetime.strptime('29 Oct 2018', '%d %b %Y')

# Get the date object from datetime object
dateObj = datetimeObj.date()

print(dateObj)
print(type(dateObj))

Output:
2018-10-29
<class 'datetime.date'>

Example 4:

Create datetime Object from time string only in ‘HH:MM:SS AP‘ format

datetimeObj = datetime.strptime('05:12:22 PM', '%I:%M:%S %p')

# Get the time object from datetime object
timeObj = datetimeObj.time()

print(timeObj)
print(type(timeObj))

Output:
17:12:22
<class 'datetime.time'>

Example 5:

Create datetime Object from timestamp scattered in a text.

Some time our timestamp is embedded in a text like,

"On January the 5th of 2018 meet me at 5 PM"

Let’s see how to convert timestamp in this string to a date time object with format codes mixed in text i.e.
textStr = "On January the 5th of 2018 meet me at 5 PM"

datetimeObj = datetime.strptime(textStr, "On %B the %dth of %Y meet me at %I %p")

print(datetimeObj)

Output:
2018-01-05 17:00:00

Complete example is as follows,
from datetime import datetime


def main():


   print("*** Convert timestamp String of format 'YYYY-MM-DDTHH::MM::SS.MICROS' to date time object ***")
   datetimeObj = datetime.strptime('2018-09-11T15::11::45.456777', '%Y-%m-%dT%H::%M::%S.%f')

   print(datetimeObj)

   print(type(datetimeObj))

   print("*** Convert timestamp String of format 'DD/MM/YYYY HH:MM:SS' to date time object ***")
   # Convert String of format 'DD/MM/YYYY HH:MM:SS' to date time object
   datetimeObj = datetime.strptime('23/Jan/2018 14:12:22', '%d/%b/%Y %H:%M:%S')

   print(datetimeObj)

   print("*** Create DateTime Object from date string only in 'DD MMM YYYY' format ***")

   datetimeObj = datetime.strptime('29 Oct 2018', '%d %b %Y')

   # Get the date object from datetime object
   dateObj = datetimeObj.date()

   print(dateObj)
   print(type(dateObj))

   print("*** Create DateTime Object from time string only in 'HH:MM:SS AP' format ***")
   datetimeObj = datetime.strptime('05:12:22 PM', '%I:%M:%S %p')

   # Get the time object from datetime object
   timeObj = datetimeObj.time()

   print(timeObj)
   print(type(timeObj))

   print('*** Create DateTime Object from timestamp scattered in a text ***')

   textStr = "On January the 5th of 2018 meet me at 5 PM"

   datetimeObj = datetime.strptime(textStr, "On %B the %dth of %Y meet me at %I %p")

   print(datetimeObj)




if __name__ == '__main__':
   main()


Output:
*** Convert timestamp String of format 'YYYY-MM-DDTHH::MM::SS.MICROS' to date time object ***
2018-09-11 15:11:45.456777
<class 'datetime.datetime'>
*** Convert timestamp String of format 'DD/MM/YYYY HH:MM:SS' to date time object ***
2018-01-23 14:12:22
*** Create DateTime Object from date string only in 'DD MMM YYYY' format ***
2018-10-29
<class 'datetime.date'>
*** Create DateTime Object from time string only in 'HH:MM:SS AP' format ***
17:12:22
<class 'datetime.time'>
*** Create DateTime Object from timestamp scattered in a text ***
2018-01-05 17:00:00