Comprehensive Jinja date and time format specifiers
Table of Contents
This is a comprehensive list of date and time format specifiers, including both zero-padded and non-zero padded versions where applicable, as well as some additional specifiers for completeness.
Comprehensive strftime and strptime format specifiers
Date specifiers
-
%a: Abbreviated weekday name (e.g., Sun, Mon). -
%A: Full weekday name (e.g., Sunday, Monday). -
%w: Weekday as a decimal number (0–6, where Sunday is 0). -
%d: Day of the month as a zero-padded decimal number (01–31). -
%-d: Day of the month as a decimal number (1–31). -
%b: Abbreviated month name (e.g., Jan, Feb). -
%B: Full month name (e.g., January, February). -
%m: Month as a zero-padded decimal number (01–12). -
%-m: Month as a decimal number (1–12). -
%y: Year without century as a zero-padded decimal number (00–99). -
%-y: Year without century as a decimal number (0–99). -
%Y: Year with century as a decimal number (e.g., 2024). -
%j: Day of the year as a zero-padded decimal number (001–366). -
%-j: Day of the year as a decimal number (1–366). -
%U: Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number (00–53). -
%-U: Week number of the year (Sunday as the first day of the week) as a decimal number (0–53). -
%W: Week number of the year (Monday as the first day of the week) as a zero-padded decimal number (00–53). -
%-W: Week number of the year (Monday as the first day of the week) as a decimal number (0–53). -
%V: ISO 8601 week number of the year (Monday as the first day of the week) as a zero-padded decimal number (01–53). -
%-V: ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (1–53).
Time specifiers
-
%H: Hour (24-hour clock) as a zero-padded decimal number (00–23). -
%-H: Hour (24-hour clock) as a decimal number (0–23). -
%I: Hour (12-hour clock) as a zero-padded decimal number (01–12). -
%-I: Hour (12-hour clock) as a decimal number (1–12). -
%p: AM or PM designation. -
%M: Minute as a zero-padded decimal number (00–59). -
%-M: Minute as a decimal number (0–59). -
%S: Second as a zero-padded decimal number (00–60). -
%-S: Second as a decimal number (0–60). -
%f: Microsecond as a decimal number, zero-padded on the left (000000–999999). -
%z: UTC offset in the form ±HHMM[SS] (e.g., +0000, -0430, +0930). -
%Z: Time zone name or abbreviation.
Combined date and time specifiers
-
%c: Locale’s appropriate date and time representation (e.g., Mon Sep 30 07:06:05 2024). -
%x: Locale’s appropriate date representation (e.g., 09/30/24). -
%X: Locale’s appropriate time representation (e.g., 07:06:05).
Miscellaneous
-
%G: ISO 8601 year with century as a decimal number (e.g., 2024). -
%g: ISO 8601 year without century as a decimal number (00–99). -
%u: ISO 8601 weekday as a decimal number (1–7, where Monday is 1). -
%%: A literal '%' character.
Examples
Here are some examples combining different specifiers for both strftime (formatting) and strptime (parsing) using Jinja2 syntax:
Formatting with strftime
Full date and time in locale’s appropriate representation:
{{ __lead_created_at | strftime('%c') }} {# Output: Mon Sep 30 07:06:05 2024 #}Full date in YYYY-MM-DD format:
{{ __lead_created_at | strftime('%Y-%m-%d') }} {# Output: 2024-09-30 #}Full time in 24-hour format:
{{ __lead_created_at | strftime('%H:%M:%S') }} {# Output: 07:06:05 #}Day of the year:
{{ __lead_created_at | strftime('%j') }} {# Output: 273 (for September 30th) #}12-hour clock with AM/PM:
{{ __lead_created_at | strftime('%I:%M %p') }} {# Output: 07:06 AM #}Non-zero padded day, month, and hour:
{{ __lead_created_at | strftime('%-d-%-m-%-H') }} {# Output: 1-9-7 (for September 1st, 07:00 AM) #}Week number of the year (Sunday as the first day):
{{ __lead_created_at | strftime('%U') }} {# Output: 39 (for the 39th week of the year) #}ISO 8601 week number and weekday:
{{ __lead_created_at | strftime('%V %u') }} {# Output: 39 1 (for the 39th week, Monday) #}
Parsing with strptime
Parsing a full date and time in locale’s appropriate representation:
{{ 'Mon Sep 30 07:06:05 2024' | strptime('%c') }}Parsing a date in YYYY-MM-DD format:
{{ '2024-09-30' | strptime('%Y-%m-%d') }}Parsing a time in 24-hour format:
{{ '07:06:05' | strptime('%H:%M:%S') }}Parsing a day of the year:
{{ '273' | strptime('%j') }}Parsing a 12-hour clock with AM/PM:
{{ '07:06 AM' | strptime('%I:%M %p') }}Parsing non-zero padded day, month, and hour:
{{ '1-9-7' | strptime('%-d-%-m-%-H') }}Parsing week number of the year (Sunday as the first day):
{{ '39' | strptime('%U') }}Parsing ISO 8601 week number and weekday:
{{ '39 1' | strptime('%V %u') }}