Generates a formatted time and date string

#include <time.h>
size_tstrftime( char * restrict s, size_t n,
                 const char * restrict format,
                 const struct tm * restrict timeptr );

The strftime() function converts date and time information from a struct tm object addressed by the last pointer argument into a character string, following a format specified by the string addressed by the pointer argument format. The strftime() function stores the resulting string in the buffer addressed by the first pointer argument, without exceeding the buffer length specified by the second argument, n. The locations that strftime() reads from and writes to using its restricted pointer parameters must not overlap.

Typically, the struct tm object is obtained by calling localtime() or gmtime(). For a description of this structure type, see mktime() in this chapter.

The generation of the output string is governed by the format string. In this way, strftime() is similar to the functions in the printf() family. The format string consists of ordinary characters, which are copied to the output buffer unchanged, and conversion specifications, which direct strftime() to convert a data item from the struct tm object and include it in the output string.

Conversion specification syntax

The conversion specifications have the following syntax:


The modifier, if present, instructs strftime() to use an alternative, locale-specific conversion for the specified data item, and is either

Get C in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.