C strftime()

The strftime() function stores time and date information, along with other information into the string pointed to by str according to the format commands found in the string pointed to by fmt and using the broken-down time pointed to by time. A maximum of maxsize characters will be placed into str.

In C99, str, fmt, and time and qualified by restrict.

The strftime() function works a little like sprintf() in that it recognizes a set of format commands that begin with the percent sign (%), and it stores its formatted output into a string. The format commands are used to specify the exact way various time and date information is represented in str. Any other characters found in the format string are placed into str unchanged. The time and date displayed are in local time. The format commands are shown in the table below. Notice that many of the commands are case sensitive.

The strftime() function returns the number of characters stored in the string pointed to by str or zero if an error occurs.

Command Replaced By
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Standard date and time string
%C Last two digits of year
%d Day of month as a decimal (1-31)
%D month/day/year (added by C99)
%e Day of month as a decimal (1-31) in two-character field (added by C99)
%F year-month-day (added by C99)
%g Last two digits of year using a week-based year (added by C99)
%G The year using a week-based year (added by C99)
%h Abbreviated month name (added by C99)
%H Hour (0-23)
%I Hour (1-12)
%j Day of year as a decimal
%m Month as decimal (1-12)
%M Minute as decimal (0-59)
%n A newline (added by C99)
%p Locale's equivalent of AM or PM
%r 12-hour time (added by C99)
%R hh:mm (added by C99)
%S Second as decimal (0-60)
%t Horizontal tab (added by C99)
%T hh:mm:ss (added by C99)
%u Day of week; Monday is first day of week (0-53) (added by C99)
%U Week of year, Sunday being first day (0-53)
%V Week of year using a week-based year (added by C99)
%w Weekday as a decimal (0-6, Sunday begin 0)
%W Week of year, Monday begin first day (0-53)
%x Standard date string
%X Standard time string
%y Year in decimal without century (0-99)
%Y Year including century as decimal
%z Offset from UTC (added by C99)
%Z Time zone name
%% The percent sign

C99 allows certain of the strftime() format commands to be modified by E or O. The E can modify c, C, x, X, y, Y, d, e, and H. The O can modify I, m, M, S, u, U, V, w, W, and y. These modifiers cause an alternative representation of the time and/or date to be displayed. Consult your compiler's documentation for details.

A week-based year is used by the %g, %G, and %V format commands. With this representation, Monday is the first day of the week, and the first week of a year must include January 4.

strftime() Syntax

Following is the syntax of the strftime() function :

size_t strftime(char *str, size_t maxsize, const char *fmt, const struct tm *time);

strftime() Example

Assuming that ltime points to a structure that contains 10:00:00 AM, the following program prints It is now 10 AM :

void main()
	struct tm *ptr;
	time_t lt;
	char str[80];
	lt = time(NULL);
	ptr = localtime(&lt);
	strftime(str, 100, "It is now %H %p", ptr);

C Online Test

« Previous Function Next Function »

Like/Share Us on Facebook 😋