How to prints every major strftime date/time format code in Python

1 Answer

0 votes
from datetime import datetime

# Use the standard library: datetime + strftime.

'''
Python strftime Format Codes:
%Y — 4‑digit year
%m — month
%d — day
%H — 24‑hour
%I — 12‑hour
%M — minutes
%S — seconds
%A — weekday name
%B — month name
%j — day of year
%V — ISO week number
%z — UTC offset
%c — locale date/time
'''

now = datetime.now()

print("=== PYTHON strftime() FORMAT TOKENS ===\n")

formats = {
    # --- Date ---
    "%Y": "4‑digit year",
    "%y": "2‑digit year",
    "%m": "Month (01–12)",
    "%B": "Full month name",
    "%b": "Short month name",
    "%d": "Day of month (01–31)",
    "%j": "Day of year (001–366)",
    "%A": "Full weekday name",
    "%a": "Short weekday name",
    "%w": "Weekday number (0=Sun, 6=Sat)",

    # --- Time ---
    "%H": "Hour (00–23)",
    "%I": "Hour (01–12)",
    "%p": "AM/PM",
    "%M": "Minutes (00–59)",
    "%S": "Seconds (00–59)",
    "%f": "Microseconds (000000–999999)",

    # --- Combined ---
    "%c": "Locale date & time",
    "%x": "Locale date",
    "%X": "Locale time",

    # --- ISO / Special ---
    "%G": "ISO 8601 year",
    "%V": "ISO 8601 week number",
    "%u": "ISO weekday (1=Mon, 7=Sun)",
    "%z": "UTC offset",
    "%Z": "Timezone name",
    "%s": "Unix timestamp (non‑standard but supported on Linux)",
}

for fmt, desc in formats.items():
    try:
        print(f"{fmt} — {desc}: {now.strftime(fmt)}")
    except Exception:
        print(f"{fmt} — {desc}: (not supported on this system)")

print("\n=== END ===")



'''
run:

=== PYTHON strftime() FORMAT TOKENS ===

%Y — 4‑digit year: 2026
%y — 2‑digit year: 26
%m — Month (01–12): 05
%B — Full month name: May
%b — Short month name: May
%d — Day of month (01–31): 20
%j — Day of year (001–366): 140
%A — Full weekday name: Wednesday
%a — Short weekday name: Wed
%w — Weekday number (0=Sun, 6=Sat): 3
%H — Hour (00–23): 15
%I — Hour (01–12): 03
%p — AM/PM: PM
%M — Minutes (00–59): 15
%S — Seconds (00–59): 45
%f — Microseconds (000000–999999): 997091
%c — Locale date & time: Wed May 20 15:15:45 2026
%x — Locale date: 05/20/26
%X — Locale time: 15:15:45
%G — ISO 8601 year: 2026
%V — ISO 8601 week number: 21
%u — ISO weekday (1=Mon, 7=Sun): 3
%z — UTC offset: 
%Z — Timezone name: 
%s — Unix timestamp (non‑standard but supported on Linux): 1779290145

=== END ===

'''

 



answered 7 hours ago by avibootz

Related questions

1 answer 208 views
1 answer 136 views
1 answer 157 views
1 answer 140 views
1 answer 159 views
1 answer 171 views
...