The Wayback Machine - https://web.archive.org/web/20230512160723/https://www.geeksforgeeks.org/python-convert-string-to-datetime-and-vice-versa/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Convert string to DateTime and vice-versa in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Write a Python program to convert a given string to DateTime in Python. 

Below are the methods that we will cover in this article:

Python Program to Convert String to DateTime 

Examples:

Input : Dec 4 2018 10:07AM 
Output : 2018-12-04 10:07:00

Input : Jun 12 2013 5:30PM 
Output : 2013-06-12 17:30:00

Using datetime.strptime() function

The strptime() is available in DateTime and time modules and is used for Date-Time Conversion. This function changes the given string of datetime into the desired format. 

Syntax of strptime function

The arguments date_string and format should be of string type. 

Syntax: datetime.strptime(date_string, format)

Code Implementation

Python3




import datetime
 
# Function to convert string to datetime
def convert(date_time):
  # The format
    format = '%b %d %Y %I:%M%p'
    datetime_str = datetime.datetime.strptime(date_time, format)
 
    return datetime_str
 
 
# Driver code
date_time = 'Dec 4 2018 10:07AM'
print(convert(date_time))
print(type(convert(date_time)))

Output:

2018-12-04 10:07:00
<class 'datetime.datetime'>)

Using dateutil module

The dateutil is a third-party module. The parsing of dates in any string format is supported by the dateutil module. Internal facts about current world time zones are provided by this module. With the release of dateutil 2.0, it was recently adapted to Python 3, along with the parser functions. parse() can be used to convert a string into the date-time format. The only parameter used is the string.

Syntax of dateutil.parser.parse function

parser.parse(parserinfo=None, **kwargs)

Code Implementation:

Python3




from dateutil import parser
 
DT = parser.parse("Jun 23 2022 07:31PM")
 
print(DT)
print(type(DT))

Output:

2022-06-23 19:31:00
<class 'datetime.datetime'>

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Program to Convert DateTime to String

Examples:

Input : 2018-12-04 10:07:00  
Output : Dec 4 2018 10:07:00AM 

Input : 2013-06-12 17:30:00Jun 12 2013 5:30PM 
Output : Jun 12 2013 5:30:00PM 

Using time.strftime

Python strftime() function is present in datetime and time modules to create a string representation based on the specified format string. 

Syntax of strftime function 

datetime_object.strftime(format_str)

Time Complexity: O(1)
Auxiliary Space: O(1)

Another similar function is available in the Python time module which converts a tuple or struct_time object to a string as specified by the format argument. 

Code Implementation:

Python3




import time
 
# Function to convert string to datetime
def convert(datetime_str):
    datetime_str = time.mktime(datetime_str)
    # The format
    format = "%b %d %Y %r" 
    dateTime = time.strftime(format, time.gmtime(datetime_str))
    return dateTime
 
 
# Driver code
date_time = (2018, 12, 4, 10, 7, 00, 1, 48, 0)
print(convert(date_time))
print(type(convert(date_time)))

Output:

Dec 04 2018 04:37:00 AM
<class 'str'>

Using datetime

Bring the datetime module in. Using the specified date and time, create a datetime object. Create a format string that outlines the formatting requirements for the datetime object. To create a string in the desired format, use the datetime object’s strftime() method. The format string is the argument for the strftime() function. Output the outcome as a string.

Python3




import datetime
 
# Create a datetime object
datetime_object = datetime.datetime(2022, 3, 14, 15, 30, 0)
 
# Define a format string
format_string = '%Y-%m-%d %H:%M:%S'
 
# Convert the datetime object to a string in the specified format
date_string = datetime_object.strftime(format_string)
 
# Print the resulting string
print(date_string)
print(type(date_string))

Output:

2022-03-14 15:30:00
<class 'str'>

My Personal Notes arrow_drop_up
Last Updated : 19 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials