C Reference ยท ctype.h, math.h and others
C strftime() function
Reference for strftime(): format a struct tm into text with %Y, %m, %d, %H, %M, %S, %A and more, handle small buffers and avoid locale surprises.
strftime() formats a broken-down time (struct tm) into a string, using conversion specifiers that start with %, much like printf. It is the standard way to produce dates for logs, file names and user interfaces: %Y-%m-%d for an ISO date, %H:%M:%S for the time of day, %A for the weekday name.
You choose the buffer and its size; strftime() returns the number of characters written, or 0 if the result (including the terminating null) would not fit.
#include <time.h> char buf[64]; struct tm *p = gmtime(&t); strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", p);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr);
| Name | Description |
|---|---|
s |
Output buffer. |
maxsize |
Size of the buffer in bytes, including room for the terminating null. |
format |
Text with % specifiers; other characters are copied unchanged. |
timeptr |
The broken-down time to format, from gmtime(), localtime() or filled in by hand. |
Returns: the number of characters placed in s, not counting the null. If the result does not fit in maxsize, it returns 0 and the contents of s are indeterminate.
Common specifiers (C locale):
| Specifier | Meaning | Example |
|---|---|---|
%Y, %y |
Year with century, two-digit year | 2023, 23 |
%m, %d, %e |
Month (01-12), day (01-31), day padded with a space | 11, 14, 14 |
%H, %I, %M, %S |
Hour 00-23, hour 01-12, minute, second | 22, 10, 13, 20 |
%p |
AM or PM | PM |
%A, %a |
Weekday name, abbreviated | Tuesday, Tue |
%B, %b |
Month name, abbreviated | November, Nov |
%j |
Day of the year (001-366) | 318 |
%F, %T, %D |
%Y-%m-%d, %H:%M:%S, %m/%d/%y |
|
%z, %Z |
UTC offset (+0000), time zone name |
|
%u, %w |
Weekday number: Monday=1..7, Sunday=0..6 | |
%U, %W, %V, %G |
Week numbers (Sunday-based, Monday-based, ISO 8601) and the ISO year | |
%c, %x, %X |
Locale's date and time, date, time | |
%n, %t, %% |
Newline, tab, percent sign |
Usage notes
- Always check the return value.
0means "did not fit" (or an empty result); using the buffer then is a bug. Size buffers generously: 64 or 128 bytes is plenty for most formats. - The function does not look at all fields. Most specifiers use only the fields they need, but
%A,%a,%jand%Ureadtm_wdayandtm_yday. If you build astruct tmby hand, callmktime()first to normalize it and fill those fields. - Names depend on the locale. The program starts in the
"C"locale, so names are English. Aftersetlocale(LC_TIME, "")the user's language applies; for machine-readable output stick to numeric specifiers or stay in the C locale. %zand%Zneed a localstruct tm. Withgmtime()the zone name isGMTand the offset+0000. Zone names are platform-specific (UTC,EST, or a long Windows name).%e,%F,%T,%D,%R,%h,%n,%tare C99 additions (older compilers may lack them). Some platform-specific flags such as%-d(no padding) are GNU extensions, not standard C.- 12-hour clock:
%Iwith%p; midnight is12:00 AM. - ISO 8601 week dates use
%G-W%V-%u, which can disagree with%Yand%Unear year boundaries. - Not a parser. The reverse operation is
strptime()(POSIX, not ISO C). Parsing dates portably usually meanssscanfand fillingstruct tmyourself. - Wide version:
wcsftime()in<wchar.h>.