This tutorial will cover some ways you can convert a datetime object to a string in python.
Converting a datetime object to a string is extremely common. For example, you may need to convert a datetime to a string when dealing with server logs or just any time you have a date that needs to be transformed into a human readable format.
Converting a datetime object to a string is something I find myself doing very frequently.
As such, I thought I'd write down a few ways that you can do this datetime to string conversion in python.
Let's get to it.
1. Datetime strftime Method
The first method for converting a datetime object to a string uses the strftime method of your datetime object.
The example looks like this:
# 01/03/2020, 19:55:48
This method takes a formatting string as a parameter and outputs a string in the specified format.
Also, please note that that if any of your formatting includes timezones, your datetime object should be timezone aware. If it is not, you will need to make your naive datetime object timezone aware. If you need to do this but are unsure of how, please read my article How To Make A Naive Datetime object Timezone Aware In Python.
Ok, now let's see another way to handle this.
2. Using the Datetime isoformat Method
The next way uses the isoformat method of the datetime object. ISO format is a widespread standard and a lot of datetime parsers will be able to easily read this. This makes serializing datetime objects to ISO format very useful. To be more precise, this method will convert your datetime object to the ISO 8601 format. If you are interested in reading further about this date format, please see the ISO 8601 Wikipedia Page.
Ok, let's see an example:
Well that was simple. And now you have your datetime as an ISO 8601 format string.
3. Using Python str Function
This method uses python's str function in order get the default string representation for a datetime object.
# 2020-01-03 19:55:48.677504
And now you have a string. You might notice that this example has the same output as the previous method. ISO format is in fact the default string representation for datetime objects.
4. Using the string format method
Another way to convert a datetime object to string is by using the python string format method.
That's pretty cool right? The datetime specifiers are placed inside a pair of braces, following a colon. And just like that, you can convert your datetime object to a string.
Now let's check out another cool method for datetime to string conversion.
5. Using f strings
The next method for converting a datetime object to a string involves python f strings. F strings were introduced in python 3.6, so this method will only work with if your python version is greater than or equal to python 3.6.
And now you have a string.
That's all folks!
If you are interested in learning how to do more with python datetime objects, check out this article on How To Convert Datetime Objects to UTC in Python.
Or if you want to check out some other great python content, here are some other python articles I wrote.