← Go Back

How to Convert a Date to a String

Using the strftime() method of datetime.date and datetime.datetime classes.

import datetime

date = datetime.date(year=2024, month=2, day=17)
date_str = date.strftime("%d/%m/%Y")
print(date_str) # 17/02/2024

The string passed to strftime() indicates the format in which you want to get the date as a string. The %d/%m/%Y format (i.e., dd/mm/yyyy) is the most common format for representing dates as strings. %d represents the day of the month between 01 (including zero) and 31; %m also represents the number of the month between 01 and 12; %Y represents the year as a four-digit number. For format codes other than %d, %m and %Y, see the official documentation.

strings dates datetime conversion

🐍 You might also find interesting: