To convert a datetime to a string in Python,you can use the strftime() function to convert. It takes a format string as an argument and returns a formatted string representation of the datetime object.
The datetime.strftime() is a built-in function that converts date and time objects to their string representation. It helps us get the datetime in the desired format of a date string. Moreover, it is in a user-readable format. This way, we can format the time in different desirable ways.
Syntax
datetime.strftime(Format_String)
Example
from datetime import datetime
# current date and time
curDT = datetime.now()
# current day
day = curDT.strftime("%d")
print("day:", day)
# current month
month = curDT.strftime("%m")
print("month:", month)
# current year
year = curDT.strftime("%Y")
print("year:", year)
# current time
time = curDT.strftime("%H:%M:%S")
print("time:", time)
# current date and time
date_time = curDT.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:", date_time)
Output
day: 21
month: 01
year: 2020
time: 13:31:14
date and time: 01/21/2020, 13:31:14
Here, day, month, year, time, and date_time are strings, whereas now is a datetime object.
The above program format codes are %d, %m, %Y, etc. The strftime() function takes one or more format codes as the argument and returns the formatted string based on it.
We imported the datetime class from the datetime module. The object of the datetime class can access the strftime() method.
How to convert Python Date to String
You can convert a date to a string using the strftime() method of the datetime module. It allows you to specify a string format for the output.
# app.py
from datetime import datetime
# current date and time
curDTObj = datetime.now()
# current date
dateStr = curDTObj.strftime("%d %b, %Y")
print("dateStr:", date)
Output
date: 21 Jan, 2020
We converted the date part of the datetime object in the above example to a string.
How to convert time to string in Python
Use the strftime()
method of the time
class from the datetime
module to convert a time to string in Python. The strftime
method allows you to specify a string format for the output.
from datetime import datetime
# current date and time
curDTObj = datetime.now()
# current time
timeStr = curDTObj.strftime("%H:%M:%S.%f")
print("Time:", timeStr)
Output
Time: 14:30:27.650890
In the above example, we have converted the time part of the datetime object to a string.
How to convert timestamp to string in Python
To convert a timestamp to a string in Python, use datetime.fromtimestamp() function. The fromtimestamp() function takes the timestamp as a parameter, returns the datetime object, and then uses the strftime() function to extract a day, month, or year from the datetime object.
Let’s say we have the following timestamp.
1579596626.421213
Example
from datetime import datetime
timestamp = 1579596626.421213
date_time = datetime.fromtimestamp(timestamp)
print("Date time object:", date_time)
fm_dt = date_time.strftime("%d/%m/%Y, %H:%M:%S")
print("Formatted Date Time:", fm_dt)
Output
Date time object: 2020-01-21 14:20:26.421213
Formatted Date Time: 21/01/2020, 14:20:26
How to convert datetime to text in Python
To convert a datetime to text in Python, you can use the strftime() function.
from datetime import datetime
# current date and time
curDTObj = datetime.now()
# current datetime
datetimeStr = curDTObj.strftime("Today is %d %B of %Y and the week day is %A at %I:%M %p")
print("Readable Text of Current Date Time:", datetimeStr)
Output
Readable Text of Current Date Time: Today is 21 January of 2020 and the week day is Tuesday at 02:35 PM
List of the date-time format in Python
Format Codes | Description | Example |
---|---|---|
%d | Day of the month as a zero-padded decimal number | 01, 02, 03, 04 …, 31 |
%a | Weekday as an abbreviated name | Sun, Mon, …, Sat |
%A | Weekday as the full name | Sunday, Monday, …, and Saturday |
%m | The month is the zero-padded decimal number | 01, 02, 03, 04, 05,…, 12 |
%b | Month as an abbreviated name | Jan, Feb, Mar,…, Dec |
%B | Month as full name | January, February, …, December |
%y | A year without a century as the zero-padded decimal number | 00, 01, 02, 03, …, 99 |
%Y | The year with century as the decimal number | 0001, …, 2018, …, 9999 |
%H | Hour (24-hour clock) as the zero-padded decimal number | 01, 02, 03, 04, 05 …, 23 |
%M | Minute is a zero-padded decimal number | 01, 02, 03, 04, 05 …, 59 |
%S | Second as a zero-padded decimal number | 01, 02, 03, 04, 05 …, 59 |
%f | A microsecond is a decimal number, zero-padded on the left | 000000, 000001, …, 999999 |
%I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, 03, 04, 05 …, 12 |
%p | Locale’s equivalent of either AM or PM | AM, PM |
%j | Day of the year as a zero-padded decimal number | 01, 02, 03, 04, 05 …, 366 |
FAQs
How to convert a datetime object to a string in Python?
You can use the strftime() function of the datetime class to convert a datetime object to a string. The strftime() method takes a format string as an argument and returns a string representation of the datetime object in the specified format.
from datetime import datetime
date = datetime.now()
string = date.strftime("%Y-%m-%d %H:%M:%S")
print(string)
Output
2023-02-07 02:17:20
What are the available format codes for strftime?
The format codes for the strftime() method specify the output string’s format.
Some common format codes include %Y for the year, %m for the month, %d for the day, %H for the hour, %M for the minute, and %S for the second.
Can I use strftime with a custom format string?
Yes, you can use strftime()
with a custom format string to specify the format of the output string.
from datetime import datetime
date = datetime.now()
string = date.strftime("%A, %B %d, %Y %I:%M %p")
print(string)
Output
Tuesday, February 07, 2023 02:19 AM
Conclusion
The easiest way to convert a datetime to a string is to use the strftime() function. The function takes in a format string and converts the datetime object to a string according to the format specified.