| Pattern | Output |
|---|---|
| “mm/dd/y” | 05/04/2018 |
| “EEEE, MMMM d, y” | Wednesday, July 4, 2018 |
| “MMM d E” | Jul 4 Wed |
| “HH:mm” | 22:05 |
| “h:mm a” | 10:05 PM |
| “EEEE, MMMM d, y ‘at’ h:mm a” | Wednesday, July 4, 2018 at 10:05 PM |
Appendix B — Datetime formatting patterns
The fmt_datetime() function supports two powerful systems for custom datetime formatting: CLDR datetime patterns and strptime format codes. This appendix provides a comprehensive reference for both.
B.1 CLDR datetime patterns
The CLDR (Common Locale Data Repository) datetime pattern system allows you to create highly customized, locale-aware datetime output. A pattern string consists of two types of elements:
Pattern fields: one or more repetitions of a specific pattern character (from the reserved sets A–Z and a–z). These fields are replaced with date and time data when formatting.
Literal text: any characters outside the reserved sets, including spaces and punctuation. Text between single vertical quotes (e.g., 'at') is output verbatim. Use two adjacent single quotes ('') to represent a literal single quote.
B.1.1 Pattern examples
The following examples use the datetime "2018-07-04T22:05:09.2358(America/Vancouver)":
B.2 Year patterns
B.2.1 Calendar year (y)
The calendar year is always numeric. The field length specifies the minimum number of digits, zero-padded as necessary. The exception is "yy", which yields only the two low-order digits.
| Pattern | Output | Notes |
|---|---|---|
| “y” | 2018 | Full year |
| “yy” | 18 | Two-digit year |
| “yyy” to “yyyyyyyyy” | 2018 to 000002018 | Zero-padded to width |
B.2.2 Week-based year (Y)
The year in week-based calendars, where year transitions occur on week boundaries. This may differ from calendar year "y" near year transitions. Used with pattern "w" for ISO 8601 week dates.
| Pattern | Output | Notes |
|---|---|---|
| “Y” | 2018 | Full year |
| “YY” | 18 | Two-digit year |
| “YYY” to “YYYYYYYYY” | 2018 to 000002018 | Zero-padded to width |
B.3 Quarter patterns
B.3.1 Quarter of year (Q and q)
Quarter names vary by width and context. The formatting form (Q) is used within complete date strings; the standalone form (q) is for independent use (e.g., calendar headers).
| Pattern | Output | Notes |
|---|---|---|
| “Q” / “q” | 3 | Numeric, one digit |
| “QQ” / “qq” | 03 | Numeric, two digits (zero-padded) |
| “QQQ” / “qqq” | Q3 | Abbreviated |
| “QQQQ” / “qqqq” | 3rd quarter | Wide |
| “QQQQQ” / “qqqqq” | 3 | Narrow |
B.4 Month patterns
B.4.1 Month (M and L)
Month names vary by width and context. The formatting form (M) is used within complete date strings; the standalone form (L) is for independent use.
| Pattern | Output | Notes |
|---|---|---|
| “M” / “L” | 7 | Numeric, minimum digits |
| “MM” / “LL” | 07 | Numeric, two digits (zero-padded) |
| “MMM” / “LLL” | Jul | Abbreviated |
| “MMMM” / “LLLL” | July | Wide |
| “MMMMM” / “LLLLL” | J | Narrow |
B.5 Week patterns
B.5.1 Week of year (w)
Values range from 1 to 53. Week 1 is the first week containing the minimum number of days specified for that locale.
| Pattern | Output | Notes |
|---|---|---|
| “w” | 27 | Minimum digits |
| “ww” | 27 | Two digits (zero-padded) |
B.5.2 Week of month (W)
Values range from 1 to 5. The first day of each month is always in week 1.
| Pattern | Output |
|---|---|
| “W” | 1 |
B.6 Day patterns
B.6.1 Day of month (d)
The day of the month, from 1 to 31.
| Pattern | Output | Notes |
|---|---|---|
| “d” | 4 | Minimum digits |
| “dd” | 04 | Two digits (zero-padded) |
B.6.2 Day of year (D)
Values range from 1 to 365 (or 366 in leap years). The field length specifies minimum digits with zero-padding.
| Pattern | Output | Notes |
|---|---|---|
| “D” | 185 | Minimum digits |
| “DD” | 185 | Zero-padded to 2 |
| “DDD” | 185 | Zero-padded to 3 |
B.6.3 Day of week in month (F)
Returns the occurrence number of the weekday within the month (e.g., “2nd Monday in March”). Days 1–7 yield 1, days 8–14 yield 2, and so on.
| Pattern | Output |
|---|---|
| “F” | 1 |
B.6.4 Modified Julian date (g)
The number of days since midnight November 17, 1858. Width specifies minimum digits with zero-padding.
| Pattern | Output |
|---|---|
| “g” to “ggggggggg” | 58303 to 000058303 |
B.7 Weekday patterns
B.7.1 Day of week name (E)
The name of the day of the week, available in four different widths.
| Pattern | Output | Notes |
|---|---|---|
| “E”, “EE”, or “EEE” | Wed | Abbreviated |
| “EEEE” | Wednesday | Wide |
| “EEEEE” | W | Narrow |
| “EEEEEE” | We | Short |
B.8 Period patterns
B.8.1 AM/PM (a)
Provides AM/PM and the narrow "a" and "p" forms.
| Pattern | Output | Notes |
|---|---|---|
| “a”, “aa”, or “aaa” | PM | Abbreviated |
| “aaaa” | PM | Wide |
| “aaaaa” | p | Narrow |
B.8.2 AM/PM with noon and midnight (b)
Provides AM/PM plus special forms for exactly noon (12:00) and midnight (00:00).
| Pattern | At Midnight | At Noon | Notes |
|---|---|---|---|
| “b”, “bb”, or “bbb” | midnight | noon | Abbreviated |
| “bbbb” | midnight | noon | Wide |
| “bbbbb” | mi | n | Narrow |
B.8.3 Flexible day periods (B)
Locale-specific phrases like “in the afternoon” or “in the evening”.
| Pattern | Morning (08:30) | Afternoon (14:00) | Notes |
|---|---|---|---|
| “B”, “BB”, or “BBB” | in the morning | in the afternoon | Abbreviated |
| “BBBB” | in the morning | in the afternoon | Wide |
| “BBBBB” | in the morning | in the afternoon | Narrow |
B.9 Hour, minute, and second patterns
B.9.1 Hour 0–23 (H)
Standard 24-hour clock where midnight is 00:00.
| Pattern | Output | Notes |
|---|---|---|
| “H” | 8 | Minimum digits |
| “HH” | 08 | Two digits (zero-padded) |
B.9.2 Hour 1–12 (h)
Standard 12-hour clock where midnight is 12:00.
| Pattern | Output | Notes |
|---|---|---|
| “h” | 8 | Minimum digits |
| “hh” | 08 | Two digits (zero-padded) |
B.9.3 Hour 1–24 (k)
Less common 24-hour clock where midnight is 24:00.
| Pattern | Output | Notes |
|---|---|---|
| “k” | 9 | Minimum digits |
| “kk” | 09 | Two digits (zero-padded) |
B.9.4 Hour 0–11 (K)
Less common 12-hour clock where midnight is 00:00.
| Pattern | Output | Notes |
|---|---|---|
| “K” | 7 | Minimum digits |
| “KK” | 07 | Two digits (zero-padded) |
B.9.5 Minute (m)
This is just the time part for the minute of the hour.
| Pattern | Output | Notes |
|---|---|---|
| “m” | 5 | Minimum digits |
| “mm” | 05 | Two digits (zero-padded) |
B.9.6 Second (s)
Provides solely the time part for the second of the minute.
| Pattern | Output | Notes |
|---|---|---|
| “s” | 9 | Minimum digits |
| “ss” | 09 | Two digits (zero-padded) |
B.9.7 Fractional second (S)
Truncates to the specified width. Add a decimal point manually to the pattern if needed.
| Pattern | Output |
|---|---|
| “S” to “SSSSSSSSS” | 2 to 235000000 |
B.9.8 Milliseconds in day (A)
The number of milliseconds since midnight (max 86,400,000).
| Pattern | Output |
|---|---|
| “A” to “AAAAAAAAA” | 439722 to 000439722 |
B.10 Era patterns
B.10.1 Era designator (G)
The Gregorian calendar has two eras: AD (Anno Domini) and BC (Before Christ).
| Pattern | Output | Notes |
|---|---|---|
| “G”, “GG”, or “GGG” | AD | Abbreviated |
| “GGGG” | Anno Domini | Wide |
| “GGGGG” | A | Narrow |
B.11 Time zone patterns
B.11.1 Specific non-location format (z)
User-friendly time zone names. Falls back to localized GMT format if unavailable.
| Pattern | Output | Notes |
|---|---|---|
| “z”, “zz”, or “zzz” | PDT | Short specific |
| “zzzz” | Pacific Daylight Time | Long specific |
B.11.2 Common UTC offset formats (Z)
ISO 8601 and localized GMT offset formats for time zones.
| Pattern | Output | Notes |
|---|---|---|
| “Z”, “ZZ”, or “ZZZ” | -0700 | ISO 8601 basic format |
| “ZZZZ” | GMT-7:00 | Long localized GMT format |
| “ZZZZZ” | -07:00 | ISO 8601 extended format |
B.11.3 Localized GMT formats (O)
GMT-based offset formats that adapt to the user’s locale.
| Pattern | Output | Notes |
|---|---|---|
| “O” | GMT-7 | Short (omits zero minutes) |
| “OOOO” | GMT-07:00 | Long (always includes minutes) |
B.11.4 Generic non-location format (v)
Useful for recurring events where specific daylight/standard distinction isn’t needed.
| Pattern | Output | Notes |
|---|---|---|
| “v” | PT | Short generic |
| “vvvv” | Pacific Time | Long generic |
B.11.5 Time zone ID and exemplar city (V)
Formats that display time zone identifiers and representative cities.
| Pattern | Output | Notes |
|---|---|---|
| “V” | cavan | Short time zone ID |
| “VV” | America/Vancouver | Long time zone ID |
| “VVV” | Vancouver | Exemplar city |
| “VVVV” | Vancouver Time | Generic location format |
B.11.6 ISO 8601 formats with Z for UTC (X)
Uses Z when the local time offset is 0.
| Pattern | Output | Notes |
|---|---|---|
| “X” | -07 | Basic (h, optional m) |
| “XX” | -0700 | Basic (h & m) |
| “XXX” | -07:00 | Extended (h & m) |
| “XXXX” | -0700 | Basic (h, m, optional s) |
| “XXXXX” | -07:00 | Extended (h, m, optional s) |
B.11.7 ISO 8601 formats without Z (x)
Same as X patterns but never uses Z for UTC.
| Pattern | Output | Notes |
|---|---|---|
| “x” | -07 | Basic (h, optional m) |
| “xx” | -0700 | Basic (h & m) |
| “xxx” | -07:00 | Extended (h & m) |
| “xxxx” | -0700 | Basic (h, m, optional s) |
| “xxxxx” | -07:00 | Extended (h, m, optional s) |
B.12 strptime format codes
As an alternative to CLDR patterns, you can use strptime-style format codes. Each code starts with % and represents a specific date or time element.
B.12.1 strptime examples
The following examples use the datetime "2015-06-08 23:05:37.48":
| Pattern | Output |
|---|---|
| “%m/%d/%Y” | 06/08/2015 |
| “%A, %B %e, %Y” | Monday, June 8, 2015 |
| “%b %e %a” | Jun 8 Mon |
| “%H:%M” | 23:05 |
| “%I:%M %p” | 11:05 pm |
| “%A, %B %e, %Y at %I:%M %p” | Monday, June 8, 2015 at 11:05 pm |
B.12.2 Date codes
Format codes for year, month, day, and week components.
| Code | Output | Description |
|---|---|---|
| “%a” | Mon | Abbreviated day of week |
| “%A” | Monday | Full day of week |
| “%w” | 1 | Day of week (0–6; Sunday = 0) |
| “%u” | 1 | Day of week (1–7; Monday = 1) |
| “%y” | 15 | Two-digit year |
| “%Y” | 2015 | Full year |
| “%b” | Jun | Abbreviated month |
| “%B” | June | Full month |
| “%m” | 06 | Month number (zero-padded) |
| “%d” | 08 | Day number (zero-padded) |
| “%e” | 8 | Day number (no padding) |
| “%j” | 159 | Day of year (zero-padded) |
| “%W” | 23 | Week number |
| “%V” | 24 | ISO 8601 week number |
| “%C” | 20 | Century number |
B.12.3 Time codes
Format codes for hour, minute, second, and AM/PM components.
| Code | Output | Description |
|---|---|---|
| “%H” | 23 | Hour (24-hour clock) |
| “%I” | 11 | Hour (12-hour clock) |
| “%M” | 05 | Minute |
| “%S” | 37 | Second |
| “%OS3” | 37.480 | Seconds with decimals (3 places shown) |
| “%p” | pm | AM/PM indicator |
B.12.4 Other codes
Additional format codes for time zones and special characters.
| Code | Output | Description |
|---|---|---|
| “%z” | +0000 | Signed time zone offset |
| “%F” | 2015-06-08 | ISO 8601 date format |
| “%%” | % | Literal percent sign |