In Python, you can convert date objects to strings using the strftime() method. The strftime() method takes a formatting string as an argument and returns a string representation of the date in the format specified by the formatting string.
Here are some examples:
import datetime# Create a date object for March 14, 2022my_date = datetime.date(2022, 3, 14)# Convert date to string in format "YYYY-MM-DD"string_date = my_date.strftime("%Y-%m-%d")print(string_date) # "2022-03-14"# Convert date to string in format "Month Day, Year"string_date = my_date.strftime("%B %d, %Y")print(string_date) # "March 14, 2022"# Convert date to string in format "MM/DD/YY"string_date = my_date.strftime("%m/%d/%y")print(string_date) # "03/14/22" |
In this example, we create a date object for March 14, 2022 and then convert it to a string in three different formats using the strftime() method. The %Y, %m, %d, %B, %d, %Y, %m, and %y are formatting codes that represent various parts of the date. You can find a full list of formatting codes in the Python documentation.