Strftime Format Builder
Build a strftime format string for Python's datetime or C's time.h by clicking tokens instead of memorising them. The preview updates as you type, each part of the format is explained, and the tool warns when a directive or flag will not work on Windows, macOS or glibc.
Last updated:
How to use the Strftime Format Builder
- Pick your language and platform: Python on Linux/macOS, Python on Windows or C with glibc.
- Click tokens in the palette to insert them at the cursor, and add separators with the small buttons or by typing.
- Check the output at the top. Change the preview date or switch between local time and UTC to test edge cases.
- Read the warnings: they flag codes that fail or behave differently on the selected platform.
- Copy the format or the generated code into your project.
Strftime reference
Examples use Saturday, March 7, 2026 at 14:05:09.123456 in UTC (C locale, English names). The last column shows where each directive works.
| Code | Meaning | Example | Works on |
|---|---|---|---|
%Y | Year with century | 2026 | All |
%y | Year without century | 26 | All |
%m | Month number, zero-padded | 03 | All |
%B / %b | Full / abbreviated month name | March / Mar | All |
%d | Day of month, zero-padded | 07 | All |
%e | Day of month, space-padded | 7 | All (Windows via the modern C runtime) |
%j | Day of the year | 066 | All |
%A / %a | Full / abbreviated weekday | Saturday / Sat | All |
%w / %u | Weekday number, Sunday = 0 / ISO, Monday = 1 | 6 / 6 | All |
%U / %W | Week of year, Sunday / Monday first | 09 / 09 | All |
%G-W%V | ISO week-based year and week | 2026-W10 | All |
%H / %I | Hour, 24-hour / 12-hour clock | 14 / 02 | All |
%p | AM or PM | PM | All |
%M / %S | Minute / second | 05 / 09 | All |
%f | Microseconds | 123456 | Python only |
%z | UTC offset | +0000 | All (Python: aware datetimes only) |
%:z | UTC offset with colon | +00:00 | Python 3.12+ |
%Z | Time zone name | UTC | All (Python: aware datetimes only) |
%F / %T | Shortcuts for %Y-%m-%d / %H:%M:%S | 2026-03-07 / 14:05:09 | All |
%k / %l / %P | Space-padded 24h / 12h hour, lowercase am/pm | 14 / 2 / pm | glibc and Python on Linux (%P not on macOS) |
%s | Seconds since 1970-01-01 UTC | 1772892309 | glibc, macOS; not Windows |
%% | A literal percent sign | % | All |
Padding flags and portability
A flag between the % and the letter changes the padding. They are extensions to the C standard, so support depends on the platform:
%-d: no padding (7). glibc, macOS and Python on those systems.%_d: pad with spaces ( 7). glibc.%^a: uppercase (SAT). glibc only.%#d: no padding on Windows. On glibc the # flag swaps letter case instead.
If one script must run on Linux, macOS and Windows, avoid the flags and build the unpadded number in code, or use a library such as Babel or Arrow. In Python, f-strings accept the same codes, so f"{dt:%Y-%m-%d}" is the same as dt.strftime("%Y-%m-%d").
Choosing a format
- Machine-readable data: ISO 8601 (
%Y-%m-%dT%H:%M:%S%z) sorts correctly as text and is understood by almost every parser. - File names:
%Y%m%d_%H%M%Shas no colons or slashes, which are not allowed in Windows file names. - HTTP headers and cookies: always format in UTC and write the literal GMT, as in
%a, %d %b %Y %H:%M:%S GMT. - People: spell the month (
%B %d, %Y) to avoid the 03/07 versus 07/03 ambiguity between US and European readers.
Need to convert an existing date between formats, or translate a pattern to moment.js or Java syntax? Use the Date Format Converter. For epoch values, see the Unix Timestamp Converter.
Frequently asked questions
What is strftime?
strftime (string format time) is the C standard library function that turns a date and time into text according to a format string. Python's datetime.strftime(), time.strftime(), PHP's strftime(), Ruby's Time#strftime and many logging tools use the same % directives, such as %Y for a four-digit year and %H for the hour.
How do I remove the leading zero from the day or hour in Python?
On Linux and macOS use a dash after the percent sign: %-d gives 7 instead of 07 and %-I gives 3 instead of 03. On Windows that raises ValueError; use %#d and %#I instead. For code that must run everywhere, format the number yourself, for example f"{dt.day}" or str(dt.hour).
What is the difference between %U, %W and %V?
%U counts weeks starting on Sunday and %W counts weeks starting on Monday; days before the first Sunday or Monday of the year are in week 00. %V is the ISO 8601 week number (01-53), where week 1 is the week containing the year's first Thursday. Pair %V with %G, the ISO week-based year, not %Y.
Why are %z and %Z empty in my Python output?
The datetime is naive, meaning it has no tzinfo. Python only fills %z and %Z for aware datetimes. Create one with datetime.now(timezone.utc) or datetime.now().astimezone(), or attach a zone with zoneinfo.ZoneInfo.
Does C strftime support microseconds?
No. %f is a Python addition; C's struct tm has no field smaller than a second. In C, get sub-second precision with clock_gettime() and append the fraction yourself.
Why does %c look different on Windows?
%c, %x and %X use the current locale. In the default C locale, glibc prints %c like 'Sat Mar 7 14:05:09 2026', while the Windows runtime prints '03/07/26 14:05:09'. Use an explicit format such as %Y-%m-%d %H:%M:%S when the exact output matters.
Can I use the same format to parse a date with strptime?
Usually yes: datetime.strptime(text, format) accepts the same codes for the common directives. It does not accept the padding flags (%-d) or shortcuts like %F, %T and %e, so the tool tells you which parts to rewrite before parsing.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.