This guide will cover some ways you can convert a string to a datetime object in python. If you are instead looking for information on how to do the opposite conversion, here’s an article on how to Convert a Datetime Object to a String in Python.

Converting a string to a datetime object is a common task in python. Whether it’s user data, a column in a csv file, a response from an api, or some other piece of data, it’s very likely that at some point, you will need to convert a string to a datetime object.

Let’s cover a 3 ways that you can do this string to datetime object conversion in python.

1. Datetime strptime Method

1
2
3
4
5
from datetime import datetime
datetime_string = "Jan 3, 2011 20:25"
datetime_format = "%b %d, %Y %H:%M"
datetime_object = datetime.strptime(datetime_string,datetime_format)
# datetime.datetime(2011, 1, 3, 20, 25)

The datetime strptime method takes two parameters: a date string and a formatting string. The formatting string will let the strptime method know how to parse your string. For reference, here’s the list of formatting directives you can use.

And as shown above, you will be returned a datetime object. Awesome!

Ok, now let’s see another way to handle string to datetime conversion in python using the arrow library.

2. Using Arrow

Arrow is a library that provides a lot of convenient functions for dealing with dates in python. I’ve talked about the arrow library quite a bit in other datetime-related guides. For example, I mentioned arrow in an article about how to Convert a Datetime Object to UTC in Python.

Here’s an example of string to datetime conversion using the arrow get function.

1
2
3
4
5
6
7
import arrow

datetime_string = "Jan 3, 2011 20:25"
datetime_format = "MMM D, YYYY H:m"
arrow_object = arrow.get(datetime_string,datetime_format)
datetime_object = arrow_object.datetime
# datetime.datetime(2011, 1, 3, 20, 25, tzinfo=tzutc())

This code snippet is relatively similar to the datetime strptime method. If you pass in a date string and a formatting string, it will return an arrow object. You can access the internal datetime object by using the appropriately named datetime property.

Simple, right?

The one thing to take note of is that arrow uses it’s own formatting symbols. Here’s the list of formatting tokens that arrow supports.

This is not a bad way to convert a string to a datetime object. However, because of the custom formatting directives that arrow uses, I would probably use the strptime method over it in this case.

Now let’s take a look at the dateutil library, which provides a really convenient and easy way to do string to datetime conversion in python.

3. Using Dateutil

1
2
3
4
from dateutil import parser
datetime_string = "Jan 3, 2011 20:25"
datetime_object = parser.parse(datetime_string)
# datetime.datetime(2011, 1, 3, 20, 25)

Awesome right? You don’t even need to specify the format. The dateutil function will figure out the appropriate format and parse your string. Using the dateutil parse function is a great choice particularly when you don’t know the format of the string your going to be passed.

That about it covers it.

If you are interested in learning how to do more with python datetime objects, check out this article on How To Make A Naive Datetime Object Timezone Aware In Python.

If you want to read some other awesome content, here are some more python articles I wrote.

Have a great day!