The %F in strftime('%F') is a shortcut for %Y-%m-%d, which formats the date as ISO 8601 format (Year-Month-Day).
For example: 2023-10-05.
What %F Does:
- Equivalent to
%Y-%m-%d
%Y: 4-digit year (e.g., 2023)
%m: 2-digit month (e.g., 10 for October)
%d: 2-digit day (e.g., 05)
Common Format Codes for strftime:
Here are other useful format codes you can use in place of %F:
Date Formats:
| Code | Meaning | Example Output |
|---|
%d | Day of month (01-31) | 05 |
%m | Month (01-12) | 10 |
%y | Short year (00-99) | 23 |
%Y | Full year | 2023 |
%a | Short weekday | Thu |
%A | Full weekday | Thursday |
%b | Short month name | Oct |
%B | Full month name | October |
%j | Day of year (001-366) | 278 |
%U | Week number (Sun week start) | 40 |
%W | Week number (Mon week start) | 40 |
%x | Locale’s date | 10/05/23 (varies) |
Time Formats:
| Code | Meaning | Example Output |
|---|
%H | Hour (00-23) | 14 |
%I | Hour (01-12) | 02 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%f | Microsecond (000000-999999) | 123456 |
%p | AM/PM | PM |
%X | Locale’s time | 14:30:45 (varies) |
Combined Date & Time:
| Code | Meaning | Example Output |
|---|
%c | Locale’s datetime | Thu Oct 5 14:30:45 2023 |
%F | ISO date (%Y-%m-%d) | 2023-10-05 |
%T | ISO time (%H:%M:%S) | 14:30:45 |
Special Characters:
| Code | Meaning | Example Output |
|---|
%% | Literal % | % |
- | Custom separator | %Y-%m-%d → 2023-10-05 |
/ | Custom separator | %m/%d/%Y → 10/05/2023 |
Example Usage:
import datetime
timestamp = 1696527000 # Example timestamp
dt = datetime.datetime.fromtimestamp(timestamp)
print(dt.strftime('%F')) # 2023-10-05
print(dt.strftime('%Y-%m-%d')) # Same as %F
print(dt.strftime('%A, %B %d, %Y')) # Thursday, October 05, 2023
print(dt.strftime('%H:%M:%S')) # 14:30:00
print(dt.strftime('%T')) # Same as %H:%M:%S
Notes:
- Locale Dependence: Codes like
%a, %A, %b, %B, %x, %X, %c depend on system locale settings.
- ISO Week/Year: Use
%G (ISO year), %V (ISO week number), %u (ISO weekday, Mon=1) for ISO week-based formatting.
- Padding: Most numeric codes (
%d, %m, %H, etc.) are zero-padded. Use %- (Linux/macOS) or %# (Windows) to remove padding (e.g., %-d for day without leading zero).
For a complete list, see Python's strftime documentation.