Converting string into DateTime in Python
Last Updated :
01 May, 2025
The goal is to convert a date string like "2021/05/25" into a Python-recognized DateTime object such as 2021-05-25 00:00:00. This enables accurate and consistent date operations like comparisons, calculations and formatting when working with time-related data from sources like files or user input. Let's understand how to do this efficiently.
Using dateutil.parser.parse()
parse() function from the dateutil library automatically detects and converts a wide range of date string formats into a datetime object. This method is ideal when your input dates are inconsistent or come from user input or APIs.
Python
from dateutil.parser import parse
s = '2023-07-25'
res = parse(s)
print(res)
Output2023-07-25 00:00:00
Explanation: Here, we passed the date string '2023-07-25' to parse(). It automatically recognized the format and returned a full datetime object.
Using datetime.strptime()
datetime.strptime() method, part of Python's datetime module, efficiently converts a date string into a DateTime object when the exact format is known, requiring a format specification like '%Y/%m/%d'.
Python
import datetime
s = '2021/05/25'
format = '%Y/%m/%d'
res = datetime.datetime.strptime(s, format)
print(res)
Output2021-05-25 00:00:00
Explanation: We define the exact format (%Y/%m/%d) for the input string '2021/05/25' and strptime() converts it into a datetime object.
Using pandas.to_datetime()
For large datasets, especially in CSV or Excel format, pandas.to_datetime() efficiently converts multiple date strings into DateTime objects, handling various formats, missing values, and errors.
Python
import pandas as pd
s = ['2021-05-25', '2020/05/25', '2019/02/15']
res = pd.to_datetime(s, format='mixed')
print(res)
OutputDatetimeIndex(['2021-05-25', '2020-05-25', '2019-02-15'], dtype='datetime64[ns]', freq=None)
Explanation: pandas.to_datetime() automatically detects the correct format for each date in the list using format='mixed'. This avoids format mismatch errors.
Using datetime.date()
For cases where you only need the date (without time), you can use datetime.strptime() followed by date() to convert the string into a DateTime object and extract the date. This method is still quite efficient but slightly more verbose than parse() or to_datetime().
Python
import datetime
s = '2021/05/25'
format = '%Y/%m/%d'
res = datetime.datetime.strptime(s, format).date()
print(res)
Explanation: After converting the string to a datetime object, we call .date() to extract just the date.
Similar Reads:
Similar Reads
Convert Array of Datetimes into Array of Strings in Python In this article, we will convert an array of Datetimes into an array of strings. we have an array whose data type is DateTime and we want to change it to string. The shape and size of the array will be the same as the input array but the data type will be different. We will begin with an introductio
4 min read
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
Create Python Datetime from string In this article, we are going to see how to create a python DateTime object from a given string. For this, we will use the datetime.strptime() method. The strptime() method returns a DateTime object corresponding to date_string, parsed according to the format string given by the user. Syntax:Â datet
4 min read
How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s
2 min read
Convert Python datetime to epoch Epoch time is a way to represent time as the number of seconds that have passed since January 1, 1970, 00:00:00 UTC. It is also known as Unix time or POSIX time and it serves as a universal point of reference for representing dates and times. Its used in various applications like file timestamps, da
2 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read