C date format string

How C's strftime format strings turn a date into text: the main conversion specifiers, locale effects, ambiguity and copy/paste pitfalls, and when to use ISO 8601.

In C, a date format string is the pattern you pass to strftime, declared in <time.h>, to turn a broken-down time (struct tm) into readable text. This review covers what that conversion changes, how locale settings affect the result, and whether the output stays unambiguous and usable after it is copied, shared or read back by software. No tool-specific test results were supplied for this article, so the guidance relies on documented standard C behavior. Gaps are flagged where behavior depends on your platform.

Short verdict: is strftime formatting suitable for your use?

For producing date text in C, strftime is the standard and appropriate choice. Its main benefit is that you describe the layout once, with conversion specifiers such as %Y, %m and %d, and the library fills in the values. Some of those values can follow the user's locale. That locale awareness is also the main tradeoff. Specifiers like %x, %c, %b and %A can produce different text depending on the active LC_TIME locale, so the same program can print different strings on different machines.

Use a fixed numeric layout such as %Y-%m-%d (ISO 8601 style) when the text will be stored, logged, sorted, compared or parsed again. Reserve locale-dependent specifiers for text that only people read. If you need both, keep the time_t value or an ISO-style string as the record and generate the localized text only for display.

Clarifying the term: which 'date format string'?

People searching for a C date format string sometimes mean a different language or system. This article covers the C standard library. Patterns used elsewhere look similar but are not interchangeable:

  • C (strftime): percent-prefixed specifiers, for example "%Y-%m-%d".
  • C# and .NET: letter patterns without percent signs, for example "yyyy-MM-dd" or "dd.MM.yyyy". In these patterns MM means month and mm means minutes.
  • ICU/CLDR-style patterns used by many localization libraries are also letter-based and do not follow C's rules.
  • printf with integer fields, for example "%04d-%02d-%02d", can also build a date string in C. Here you supply the numbers yourself, so you also have to apply the struct tm offsets yourself.

Pasting a C# or ICU pattern into strftime will not work as intended. Letters without a percent sign are copied literally, so "yyyy-MM-dd" prints exactly those characters.

What the conversion actually changes

strftime does not change the stored date. It reads the fields of a struct tm and writes characters into a buffer you provide. The value in time_t or struct tm stays the same, and only its textual presentation is produced. This distinction matters when deciding what to keep. The string is a rendering, and some renderings lose information. A two-digit year or a format without a time zone cannot be turned back into the exact original moment.

Two struct tm details often cause off-by-one or off-by-1900 errors when people format dates by hand instead of using strftime. tm_year counts years since 1900, and tm_mon runs from 0 (January) to 11. strftime applies these offsets for you, which is one reason to prefer it over assembling dates with printf.

The function signature and its return value:

size_t strftime(char *s, size_t maxsize,
                const char *format, const struct tm *timeptr);

It returns the number of characters written, not counting the terminating null. If the result would not fit in maxsize, it returns 0 and the buffer contents are indeterminate. A return of 0 can also mean the formatted result was legitimately empty, for example a format of only %p in a locale with no AM/PM strings. Check the return value and size the buffer generously. In standard C, an unrecognized conversion specifier is undefined behavior, so do not rely on unsupported letters being printed literally.

Common conversion specifiers

The outputs below are for 4 March 2026 at 14:05:09, a Wednesday, in the default "C" locale. In other locales, the name-based and locale-composite specifiers will differ.

Specifier | Meaning | Output in "C" locale
%Y | Four-digit year | 2026
%y | Last two digits of year | 26
%m | Month, 01–12 | 03
%d | Day of month, 01–31 | 04
%e | Day of month, space-padded (C99) | ' 4'
%H | Hour, 00–23 | 14
%I | Hour, 01–12 | 02
%M | Minute, 00–59 | 05
%S | Second, 00–60 | 09
%p | AM/PM designation (locale) | PM
%a | Abbreviated weekday name (locale) | Wed
%A | Full weekday name (locale) | Wednesday
%b | Abbreviated month name (locale) | Mar
%B | Full month name (locale) | March
%j | Day of year, 001–366 | 063
%u | ISO weekday, Monday = 1 (C99) | 3
%F | Same as %Y-%m-%d (C99) | 2026-03-04
%T | Same as %H:%M:%S (C99) | 14:05:09
%D | Same as %m/%d/%y (C99) | 03/04/26
%x | Locale's date representation | 03/04/26
%c | Locale's date and time representation | Wed Mar 4 14:05:09 2026
%% | A literal percent sign | %

Time zone specifiers are left out of the table because their output depends on the system. %z gives the offset from UTC, such as -0500 (C99), and %Z gives the zone name or abbreviation. Neither has a fixed value for this example. Specifiers marked C99 may be missing from older or non-conforming C libraries, so confirm them in your library's documentation.

A minimal, checkable example

This program builds the example date and prints it in ISO style and in the C locale's own date format. mktime normalizes the structure and fills in the weekday and day-of-year fields. It interprets the fields as local time.

#include <stdio.h>
#include <time.h>

int main(void)
{
    struct tm t = {0};
    t.tm_year  = 2026 - 1900;  /* years since 1900 */
    t.tm_mon   = 3 - 1;        /* 0 = January */
    t.tm_mday  = 4;
    t.tm_hour  = 14;
    t.tm_min   = 5;
    t.tm_sec   = 9;
    t.tm_isdst = -1;           /* let mktime decide */

    if (mktime(&t) == (time_t)-1) {
        fputs("mktime failed\n", stderr);
        return 1;
    }

    char buf[64];
    if (strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", &t) == 0) {
        fputs("buffer too small\n", stderr);
        return 1;
    }
    puts(buf);   /* 2026-03-04 14:05:09 */

    if (strftime(buf, sizeof buf, "%x", &t) > 0)
        puts(buf);   /* 03/04/26 in the "C" locale */

    return 0;
}

How locale changes the output

A C program starts in the "C" locale. Month and weekday names stay in English, and %x uses month/day/year order, until the program calls setlocale. Calling setlocale(LC_TIME, "") adopts the user's environment settings. Calling it with a named locale requests a specific one, for example a Russian locale for readers who expect Russian month names. Locale names and availability differ between operating systems, and a locale that is not installed makes setlocale return NULL. Check that return value instead of assuming the change worked.

Some languages need different word forms depending on context. In Russian, for example, a month name inside a date is grammatically different from the month name on its own. Some C libraries expose alternative forms through the O modifier, such as %OB, but support and behavior vary by library and version. Verify the actual output on your target system before relying on it, and do not assume that one locale's correct rendering means others behave the same way.

Character encoding follows the locale too. In a UTF-8 locale, localized names come out as UTF-8. In a locale that uses a legacy encoding, the same names come out as different bytes, which can show up as garbled text when pasted into a UTF-8 destination.

Will the result stay readable and usable after copying or sharing?

strftime produces ordinary characters, not styling or visual effects, so copy and paste carries the text over exactly as generated. The risks come from what the text means and how other software handles it, not from the copying itself.

  • Ambiguity: 03/04/26 means March 4 to a US reader and 3 April to many readers elsewhere. Two-digit years add further doubt. For shared or international text, use %Y-%m-%d or spell out the month with %B.
  • Sorting and search: year-month-day strings with zero padding (%Y-%m-%d) sort correctly as plain text. Locale formats and month names do not.
  • Parsing back: locale-dependent text is hard to read back reliably. strptime, the usual parsing counterpart, is defined by POSIX rather than ISO C and is not available everywhere. If the text must be parsed later, keep a fixed numeric format.
  • Time zones: a timestamp without %z, or without stating that it is UTC, can be misread when shared across regions.
  • Week-based years: %G (ISO week-based year) and %Y (calendar year) differ near year boundaries. 1 January 2027 falls in ISO week 53 of 2026, so a format like "%G-W%V" gives 2026-W53 while %Y gives 2027. Do not mix %G with %m or %d.

Accessibility: no documentation or scoped test of how particular screen readers announce numeric dates was supplied for this article, so no claim is made about specific assistive technology. As a general principle, a date with the month written out is less ambiguous for any reader than an all-numeric form. If accessibility matters for your output, test the exact strings with the screen readers your audience uses, or provide a written-out form alongside the numeric one. For HTML output, the time element with a machine-readable datetime attribute is a standard way to pair the two.

When a plain numeric format is the better choice

  • Log files, file names and database fields: use %Y-%m-%d, or %Y-%m-%dT%H:%M:%S with an explicit offset or a stated UTC convention.
  • Data exchanged between systems or regions: avoid %x, %c and month names, since their output depends on the sender's locale.
  • Anything another program will parse: keep the layout fixed and documented.
  • User-facing display: locale formats are appropriate here, as long as the stored value stays in a fixed form.

Open questions to check on your platform

  • Whether your C library supports the C99 specifiers (%F, %T, %e, %z, %G, %V, %u) and any E or O modifiers you plan to use.
  • Which locale names are installed, and whether they produce UTF-8 output.
  • The exact %c, %x and %X output for each locale you target, since it is implementation-defined.
  • How your chosen output reads in the destination applications and assistive technologies your audience actually uses.