Python strftime()

All assignments assume you import like:

import datetime
now = datetime.datetime.now()

1) Format as β€œYYYY-MM-DD”

Print the current date as:

2026-02-27

2) Format as β€œDD/MM/YYYY”

Print date like:

27/02/2026

3) Format as β€œMonthName DD, YYYY”

Print:

February 27, 2026

4) Format Date & 24h Time

Print:

Example:


5) Only Time (24-Hour)

Format to show:


6) Only Time (12-Hour)

Print time like:


7) Day of Week Name

Print:


8) Abbreviated Day of Week

Print:


9) Month as Number

Print only the month number (02).


10) Month Short Name

Print:


11) Month Full Name

Print:


12) Day of Year

Print which day of year it is (1–365/366).


13) ISO Week Number

Print the week number of the year.


14) Year Two-Digit

Print year as:


15) Zero-Padded Day & Month

Ensure both day and month are zero-padded (02, 27).


16) Mixed Format

Print:


17) Full DateTime With Timezone

Assume now has tz info; format as:

(You can skip setting tz for beginners.)


18) Show AM/PM Only

Print:

or


19) Include Weekday & Time

Print:


20) Custom Combined Format

Ask user for a date and print it as:

Example:


🧠 Helpful strftime Codes

Format
Meaning

%Y

Year with century (2026)

%y

Year without century (26)

%m

Month as zero-padded number

%b

Abbreviated month name

%B

Full month name

%d

Day of month

%H

Hour (24-hr)

%I

Hour (12-hr)

%M

Minute

%S

Second

%p

AM/PM

%A

Weekday full

%a

Weekday abbreviated

%j

Day of year

%U

Week number (Sun first)

%W

Week number (Mon first)


πŸ“ Quick Example


πŸ“ Tips for Students

βœ… Use strftime to format into readable text βœ… Memorize the most common codes: %Y, %m, %d, %H, %M, %S, %p, %A, %B βœ… Use combinations to produce meaningful displays


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated