Whenever we deal with date and time information stored as strings, converting it to DateTime objects can prove to be very useful. For instance, when we need to import data from sources like CSV files, JSON files, or databases which may be stored as strings, converting to DateTime objects allows us to manipulate the data precisely or with more flexibility.
Also Read: How to Convert CSV to NumPy Array in Python
Converting String to DateTime in Python
To convert a string to a datetime object in Python, two commonly used functions are strptime() and parse(). Let’s look at them one by one.
Converting String to DateTime Using strptime()
The strptime() function in Python is part of the DateTime module and is used to parse (convert) a string representation of a date and time into a DateTime object. The name “strptime” stands for “string parse time.”
from datetime import datetime datetime.strptime(date_string, format)
- date_string: The string containing the date and time information that we want to parse.
- format: A format string specifying the expected format of the date_string. It tells Python how to interpret the various components (year, month, day, hour, etc.) in the string.
The strptime() function creates a DateTime object based on the provided date_string and format.
Date and Time Format Codes:
- %b: Month as locale’s abbreviated name.
- %d: Day of the month as a zero-padded decimal number.
- %Y: Year with century as a decimal number.
- %I: Hour (12-hour clock) as a zero-padded decimal number.
- %M: Minute as a zero-padded decimal number.
- %p: Locale’s equivalent of either AM or PM
from datetime import datetime date = "Oct 14 1997 7:15AM" print(type(date)) date_time = datetime.strptime(date,"%b %d %Y %I:%M%p") print(date_time) print(type(date_time))
In the above code, to use the strptime() function, we first imported the DateTime module. Then we have created a string and stored it into a variable date. After that, we have printed the type of the variable date. Then we have provided the parameters string as date and format as %b %d %Y %I:%M%p and saved into the variable date_time. In last we have printed the variable date_time and the type of the date_time.
In the output, we got the date variable type as a string as we provided the date and time in the form of a string in the variable date. Also, we got the required DateTime object with its type.
Converting String to DateTime Using parse()
For parsing strings into DateTime objects, the dateutil module in Python gives us a very flexible way which is by using the parse() function. In this function, we are not required to specify the format of the string like strptime() function as it can recognize a broad range of date and time formats by itself.
from dateutil import parser parser.parse(date_string)
- date_string: The string containing the date and time information that we want to parse into a DateTime object.
The parse() function creates a DateTime object based on the provided date_string.
from dateutil import parser date_time = parser.parse("Oct 14 1997 7:15AM") print(date_time) print(type(date_time))
In the above code, to use the parse() function, we first imported the dateutil module. Then we passed the string directly into the parser.parse() function and we don’t have to specify the format this time like in the strptime() function. Finally, we have printed the variable date_time and its type.
We have successfully converted using this function as well.
Difference Between strptime() and parse() Function
For strptime() function we need to import datetime module but for the parse() function we have to import dateutil module.
If we talk about simplicity then no doubt it will be the parse() function which is simpler because in strptime() function we always need to provide the format of the input string but in the parse() it is not like that, parse() will automatically recognize the format of the string which we provided in the parse() function. Also, sometimes strptime() function causes an error if we don’t provide the string format that matches the string but the parse() function is forgiving it can deal with the unexpected format of the date string and covert it into the DateTime object.
Both the functions are good and they have their own importance but my personal favourite is the parse() function as it is more simple and flexible because we don’t need to provide any additional argument, we can directly convert a string into a DateTime object by just passing the string into it.
In this tutorial, we have discussed strptime() method provided by DateTime module and the parse() method provided by dateutil module to convert string to DateTime. After reading this tutorial, we hope you can easily convert a string into a DateTime object in Python