Python string to datetime Conversion

There are several ways to convert string to datetime in Python. Let’s take a look at each of these with examples.

Convert Python String to datetime using datetime Module

We can convert a string to datetime using the strptime() function. The strptime() function is available in Python’s datetime and time module and can be used to parse a string to datetime objects.

Syntax

datetime.strptime(date_string, format)

Parameters

The strptime() class method takes two arguments:

  • date_string (that be converted to datetime)
  • format code

Let’s take a few examples to demonstrate the strptime() function use case.

Python String to datetime

# Python program to convert string to datetime

#import datetime module
from datetime import datetime

# datetime in string variable
date_str = '22/11/21 03:15:10'

# covert string into datetime using strptime() function
date_obj = datetime.strptime(date_str, '%d/%m/%y %H:%M:%S')

print("The type of the date is now",  type(date_obj))
print("The date is", date_obj)

Output

The type of the date is now <class 'datetime.datetime'>
The date is 2021-11-22 03:15:10

Python String to date

If you want to convert the string into date format, we can use the date() function and the strptime() function, as shown in the example below.

# Python program to convert string to date object

#import datetime module
from datetime import datetime

# datetime in string variable
date_str = '22/11/21 03:15:10'

# covert string into datetime using strptime() function
date_obj = datetime.strptime(date_str, '%d/%m/%y %H:%M:%S').date()

print("The type of the date is now",  type(date_obj))
print("The date is", date_obj)

Output

The type of the date is now <class 'datetime.date'>
The date is 2021-11-22

Python String to time

If you want to convert the string into date format, we can use the time() function and the strptime() function, as shown in the example below.

# Python program to convert string to datetime

#import datetime module
from datetime import datetime

# datetime in string variable
date_str = '22/11/21 03:15:10'

# covert string into datetime using strptime() function
date_obj = datetime.strptime(date_str, '%d/%m/%y %H:%M:%S').time()

print("The type of the date is now",  type(date_obj))
print("The date is", date_obj)

Output

The type of the date is now <class 'datetime.time'>
The date is 03:15:10

Convert Python String to datetime using dateutil

The other way to convert string to datetime is using the dateutil module. The only parameter it takes is the string object and converts it into a datetime object. 

Example

# Python program to convert string to datetime

#import dateutil module
from dateutil import parser

# datetime in string variable
date_str = '22/11/21 03:15:10'

# covert string into datetime using parse() function
date_obj = parser.parse(date_str)

print("The type of the date is now",  type(date_obj))
print("The date is", date_obj)

Output

The type of the date is now <class 'datetime.datetime'>
The date is 2021-11-22 03:15:10
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post
Python Print Variable

Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod…
View Post