Format a date

EasyDatetime Programs · 2 of 20

Write a Python program to print a date in several formats.

Advertisement
Output
%d-%m-%Y   02-11-2026
%d/%m/%y   02/11/26
%B %d, %Y  November 02, 2026
%a %d %b   Mon 02 Nov

Approach

  1. strftime codes.

Solution

Python
from datetime import date d = date(2026, 11, 2)for fmt in ["%d-%m-%Y", "%d/%m/%y", "%B %d, %Y", "%a %d %b"]:    print(fmt.ljust(10), d.strftime(fmt))
Output
%d-%m-%Y   02-11-2026
%d/%m/%y   02/11/26
%B %d, %Y  November 02, 2026
%a %d %b   Mon 02 Nov
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs