Formatting Dates
Build readable date strings.
Shaping the Output
You already know os.date can format time. This lesson focuses on the format string itself: the small % codes that decide exactly how a date appears.
Mastering them lets you produce dates in any style, from 2024-03-09 to Saturday, March 9.
print(os.date("%Y-%m-%d"))Year, Month, Day
The core date codes are %Y for the 4-digit year, %m for the 2-digit month (01-12), and %d for the 2-digit day (01-31).
Anything that is not a code, like the dashes here, is printed exactly as written.
print(os.date("%Y/%m/%d"))
print(os.date("%d-%m-%Y"))All lessons in this course
- os.time and os.date
- Formatting Dates
- Time Arithmetic
- Measuring Durations