0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert datetime to String Easily

Use the strftime method of a datetime object with a format string, like datetime_obj.strftime('%Y-%m-%d %H:%M:%S'), to convert datetime to string in Python.
📋

Examples

Inputdatetime.datetime(2024, 6, 1, 15, 30, 45)
Output"2024-06-01 15:30:45"
Inputdatetime.datetime(2020, 1, 1, 0, 0, 0)
Output"2020-01-01 00:00:00"
Inputdatetime.datetime(1999, 12, 31, 23, 59, 59)
Output"1999-12-31 23:59:59"
🧠

How to Think About It

To convert a datetime to a string, think about how you want the date and time to look as text. You use the strftime method, which lets you pick a pattern for the output, like year-month-day or hour:minute:second. This method turns the datetime into a readable string.
📐

Algorithm

1
Get the datetime object you want to convert.
2
Choose the string format you want using format codes like %Y for year, %m for month, %d for day.
3
Call the <code>strftime</code> method on the datetime object with your format string.
4
Return or print the resulting string.
💻

Code

python
from datetime import datetime

# Create a datetime object
now = datetime(2024, 6, 1, 15, 30, 45)

# Convert datetime to string
string_date = now.strftime('%Y-%m-%d %H:%M:%S')
print(string_date)
Output
2024-06-01 15:30:45
🔍

Dry Run

Let's trace converting datetime(2024, 6, 1, 15, 30, 45) to string.

1

Create datetime object

now = datetime(2024, 6, 1, 15, 30, 45)

2

Call strftime with format

string_date = now.strftime('%Y-%m-%d %H:%M:%S')

3

Print the string

Output: '2024-06-01 15:30:45'

StepActionValue
1Create datetime2024-06-01 15:30:45
2Convert to string'2024-06-01 15:30:45'
3Print output2024-06-01 15:30:45
💡

Why This Works

Step 1: Using strftime method

The strftime method formats the datetime object into a string based on the format codes you provide.

Step 2: Format codes define output

Codes like %Y for year and %m for month tell Python how to build the string.

Step 3: Result is a string

The method returns a string that represents the datetime in the format you chose.

🔄

Alternative Approaches

Using isoformat() method
python
from datetime import datetime
now = datetime(2024, 6, 1, 15, 30, 45)
string_date = now.isoformat()
print(string_date)
This returns an ISO 8601 string like '2024-06-01T15:30:45', which is standard but less customizable.
Using str() function
python
from datetime import datetime
now = datetime(2024, 6, 1, 15, 30, 45)
string_date = str(now)
print(string_date)
This converts datetime to string with default format 'YYYY-MM-DD HH:MM:SS.microseconds', less control over format.

Complexity: O(1) time, O(1) space

Time Complexity

Converting datetime to string with strftime is a fixed-time operation, so it runs in constant time.

Space Complexity

The operation creates a new string, so space used is proportional to the length of the output string, which is constant for fixed formats.

Which Approach is Fastest?

strftime and isoformat are both very fast; str() is simplest but less flexible.

ApproachTimeSpaceBest For
strftimeO(1)O(1)Custom date/time string formats
isoformatO(1)O(1)Standard ISO 8601 format
str()O(1)O(1)Quick default string conversion
💡
Use strftime with format codes to get exactly the string format you want.
⚠️
Forgetting to use strftime and trying to convert datetime directly with str() expecting a custom format.