Converting string into DateTime in Python Last Updated : 01 May, 2025 Comments Improve Suggest changes Like Article Like Report 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) Output2021-05-25 Explanation: After converting the string to a datetime object, we call .date() to extract just the date.Similar Reads:Convert string to DateTime and vice-versa in PythonManipulate Date and Time with the Datetime ModuleCreate Python Datetime from stringWorking with Datetime Objects and Timezones in PythonIntroduction to Python Dateutil Packagedatetime.strptime()pandas.to_datetime() Comment More infoAdvertise with us Next Article Converting string into DateTime in Python sravankumar_171fa07058 Follow Improve Article Tags : Python Python-datetime Practice Tags : python 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 Parsing DateTime strings containing microseconds in Python Most of the applications require a precision of up to seconds but there are also some critical applications that require nanosecond precision, especially the ones which can perform extremely fast computations. It can help provide insights on certain factors related to time space for the application. 3 min read How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f 3 min read Convert Datetime to UTC Timestamp in Python Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it's often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this ar 3 min read How to convert Python's .isoformat() string back into datetime object In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current tim 1 min read Like