Home C time.h Functions strftime() C strftime() Function 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:
strftime(text, sizeof text, "%Y-%m-%d", &when); Call What 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 size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr); Parameters Parameter Description s Destination character buffer. maxsize Capacity of s, including room for '\0'. format Format string containing ordinary text and % directives. timeptr Pointer to the broken-down time to format.
Return Value Return Value Description Positive size_t Number of characters written, excluding the null terminator. 0 The formatted result did not fit, or the format produced an empty result.
C Examples 1. Format an ISO Date 2. Format Date and Time 3. Detect a Small Buffer Example 1 — Format an ISO Date This example demonstrates strftime() with a small, complete program and checks the relevant result before using it.
#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 Step 1. Initialize a broken-down date.Step 2. Allocate enough room for ten visible characters and the terminator.Step 3. The three directives expand to four-digit year, month, and day.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.
#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 Step 1. Set both calendar and clock fields.Step 2. Choose a day-first format with literal separators.Step 3. %H uses a 24-hour clock and the other clock directives use two digits.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.
#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 Step 1. Create a date that would need ten visible characters.Step 2. Provide only five bytes of destination capacity.Step 3. strftime() cannot store the complete formatted string and terminator.Step 4. The returned zero signals truncation instead of a usable partial date.Frequently Asked Questions What does strftime() do in C? strftime() formats broken-down date and time into a caller-provided text buffer.
Which header declares strftime()? Include <time.h> before calling strftime().
What does strftime() return? On success it returns the number of characters written, not counting the null terminator. On failure or truncation it returns 0.
What should beginners check when using strftime()? Always check for 0 before using the buffer as newly formatted output. Directive wording such as month names can depend on the active locale.
Is strftime() portable? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion