Python Tutorial: Turning dates into strings

Learn to convert Python date objects to strings using isoformat() for ISO 8601 format or strftime() for custom layouts. isoformat() outputs YYYY-MM-DD, useful for CSV files and filenames where lexical sorting matches chronological order. strftime() accepts format codes like %Y, %m, %d to create any date string you need.
Chapters
Python has a very flexible set of tools for turning dates back into strings to be easily read. We want to put dates back into strings when, for example, we want to print results, but also if we want to put dates into a file name or if we want to write dates out to a CSV or Excel file. For example, let's create a date and see how Python prints it by default. As before, we import date from datetime and let's again create an object for November 5, 2017. When we ask Python to print the date, it prints the year, day, and then the month separated by dashes and always with two digits for the day and month. In the comment you can see that I've noted this as yyyy 4 digit year, 2 digit month and 2 digit day of the month. This default format is also known as ISO format or ISO 8601 format after the international standard ISO 8601 that it is based on. ISO 8601 strings are always the same length, since month and day are written with zeros when they are less than 10. We'll talk about another advantage of ISO 80601 in a moment. If we want the ISO representation of a date as a string, say to write it to a CSV file instead of just printing it, you can call the isoformat method. In this example, we put it inside a list so that you can see it creates a string. The ISO8601 format has another nice advantage. To demonstrate, we've created a variable called sumdates and represented two dates here as strings January 1, 2000 and December 31, 1999. Dates formatted as ISO 8601 strings sort correctly. When we print the sorted version of this list, the earlier day is first and the later day is second. For example, if we use ISO 8601 dates in filenames, they can be correctly sorted from earliest to latest. If we had used month or day first, the strings would not sort in chronological order. If you don't want to put dates in ISO8601 format, Python has a flexible set of options for representing dates in other ways using the strftime method. Strftime works by letting you pass a format string which Python uses to format your date. Let's see an example. We again create an example date of January 5, 2017. We then call Sturf time on D with the format string of percent capital Y. STRF time reads the percent in capital Y and fills in the year in this string for us. Strf time, though, is very flexible. We can give it arbitrary strings with percent capital y in them for the format string, and it will stick the year in. For example, we can use the format string of year is percent Y. StRF time has other placeholders besides percent yes percent M gives the month and percent D gives the day of the month. Using these, we can represent dates in arbitrary formats for whatever our needs are. In this lesson, we discussed how Python can represent a date as a string. We emphasized the importance and utility of ISO8601 format, but also introduced Strftime, which lets you turn dates into a wide variety of strings depending on your needs.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







