C strftime() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C strftime() function writes formatted date and time text into a character buffer. Directives such as %Y, %m, %d, %H, %M, and %S select the fields to include.

You choose the format, destination, and capacity. The function returns the number of characters written before the null terminator, or 0 when the result does not fit (and in a few format-dependent cases). Include <time.h>.

Syntax

Start with this concrete strftime() call:

C
strftime(text, sizeof text, "%Y-%m-%d", &when);
CallWhat it does
strftime(text, sizeof text, "%Y-%m-%d", &when);Formats when as a numeric date in text.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr);

Parameters

ParameterDescription
sDestination character buffer.
maxsizeCapacity of s, including room for '\0'.
formatFormat string containing ordinary text and % directives.
timeptrPointer to the broken-down time to format.

Return Value

Return ValueDescription
Positive size_tNumber of characters written, excluding the null terminator.
0The formatted result did not fit, or the format produced an empty result.

C Examples

Example 1 — Format an ISO Date

This example demonstrates strftime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    struct tm when = {0};
    when.tm_year = 124;
    when.tm_mon = 0;
    when.tm_mday = 7;
    char text[16];
    if (strftime(text, sizeof text, "%Y-%m-%d", &when) > 0)
        printf("%s\n", text);
    return 0;
}

How It Works

  1. Step 1. Initialize a broken-down date.
  2. Step 2. Allocate enough room for ten visible characters and the terminator.
  3. Step 3. The three directives expand to four-digit year, month, and day.
  4. Step 4. A positive return confirms that text contains the complete result.

Example 2 — Format Date and Time

This example demonstrates strftime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    struct tm when = {0};
    when.tm_year=124; when.tm_mon=10; when.tm_mday=17;
    when.tm_hour=20; when.tm_min=15; when.tm_sec=9;
    char text[32];
    if (strftime(text, sizeof text, "%d/%m/%Y %H:%M:%S", &when))
        printf("%s\n", text);
    return 0;
}

How It Works

  1. Step 1. Set both calendar and clock fields.
  2. Step 2. Choose a day-first format with literal separators.
  3. Step 3. %H uses a 24-hour clock and the other clock directives use two digits.
  4. Step 4. The nonzero return allows the completed buffer to be printed.

Example 3 — Detect a Small Buffer

This example demonstrates strftime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    struct tm when = {0};
    when.tm_year = 124;
    when.tm_mday = 1;
    char tiny[5];
    size_t count = strftime(tiny, sizeof tiny, "%Y-%m-%d", &when);
    printf("characters written: %zu\n", count);
    return 0;
}

How It Works

  1. Step 1. Create a date that would need ten visible characters.
  2. Step 2. Provide only five bytes of destination capacity.
  3. Step 3. strftime() cannot store the complete formatted string and terminator.
  4. Step 4. The returned zero signals truncation instead of a usable partial date.

Frequently Asked Questions

strftime() formats broken-down date and time into a caller-provided text buffer.
Include <time.h> before calling strftime().
On success it returns the number of characters written, not counting the null terminator. On failure or truncation it returns 0.
Always check for 0 before using the buffer as newly formatted output. Directive wording such as month names can depend on the active locale.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: time()

Continue through the C time.h function tutorials.

time() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful