Date Formatting in Python
Let's now output the date in a specific format, for example, year-month-day.
To do this, we declare a variable dt, in which the date we need will be obtained via the datetime method of the datetime module. Then we need to apply the strftime method to the date written in dt. The date format symbol, written using the % command and the format symbol, is passed to the parameters of this method. If we need to output year-month-day, then the string '%Y-%m-%d' is passed to the method parameter:
dt = datetime.datetime(2025, 12, 31, 12, 59, 59)
res = dt.strftime('%Y-%m-%d')
print(res) # 2025-12-31
The strftime method can be used to output various date formats. The full list can be found here. Let's, for example, also output the day of the week by specifying the special symbol '%w' in the parameter:
res = dt.strftime('%Y-%m-%d %w')
print(res) # 2025-12-31 3
Print the current date to the console in the format day.month.year.
Output the current time to the console in the following format: hours:minutes:seconds year/month moon m. mo./day:day of the week.