Syntax
The format for the strftime function is presented as follows.
Size_tstrftime(char *str, size_tmaxsize, const char *format, const struct tm *timeptr)
The parameters or arguments used:
It serves as a reference to the destination array where the duplicated C string is stored after the operation.
Maxsize −
It represents the maximum number of characters that can be copied into the str variable.
Format −
It's the C string that can have a blend of alphabetical and numerical characters along with distinct format placeholders. The function substitutes these format placeholders with the accurate values to display the provided time in the tm structure.
These are the format specifiers:
| Specifier | Replaced by | Example |
|---|---|---|
%a |
Weekday's abbreviated name | Mon |
%A |
Weekday's full name | Monday |
%b |
Shortened month name | Feb |
%B |
Month's full name | February |
%c |
Display of time and date | Sun Aug 19 02:56:02 2012 |
%d |
(01-31) Day of the month | 19 |
%H |
Hour (00-23) in 24-hour format | 14 |
%I |
(01-12) hour in a 12-hour format | 05 |
%j |
Date (from 001 to 366) | 231 |
%m |
Month expressed as a decimal (01-12) | 08 |
%M |
(Minute 00:59) | 55 |
%p |
AM or PM time zone | PM |
%S |
(00-61)second | 02 |
%U |
Week number, with the first Sunday serving as week one's first day (00-53) | 33 |
%w |
Sunday is represented by the number 0 (0-6) on a weekday. | 4 |
%W |
Week number, with the first Monday serving as week one's first day (00-53) | 34 |
%x |
Representing dates | 08/19/12 |
%X |
Representation of time | 02:50:06 |
%y |
Last two digits of the year (00-99) | 01 |
%Y |
Year | 2012 |
%Z |
Name or shorthand for the time zone | CDT |
%% |
A% symbol | % |
The timeptr -
It is a pointer that references a tm structure containing the elements of a time measurement, as illustrated below.
Struct tm {
Int time_sec; /* seconds, from 0 to 59 */
Int time_min; /* minutes, 0 to 59. */
Int time_hour; /* hours between 0 and 23 */
Int time_mday; /* range of 1 to 31 days in the month */
Int time_mon; /* range from 0 to 11 months */
Int time_year; /* how many years have passed since 1900 */
Int time_wday; /* weekday with a value between 0 and 6 */
Int time_yday; /* has a value between 0 and 365. */
Int time_isdst; /* daytime extending hours */
};
Return Value
The function returns the count of characters copied to the string "str" (excluding the null-terminator) when the created C string fits within the specified size limit, including the null-terminator. If the string exceeds the size limit, it returns zero.
Example
The strftime function is employed in the following example.
#include <stdio.h>
#include <time.h>
int main () {
time_trawtime;
struct tm *info;
char buffer1[100];
time( &rawtime );
Info = localtime( &rawtime );
strftime(buffer1,80,"%x - %I:%M%p", info);
printf("The Formatted date & time is : |%s|\n", buffer1 );
return(0);
}
Output:
The Formatted date & time is : |06/03/23 - 05:17PM|
Example:2
// A C program that demonstrates how strftime() works
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define Size 50
int main ()
{
time_tt ;
struct tm *tmp ;
char MY_TIME[Size];
time( &t );
//In order to fill a tm structure with values that correspond to the local time, localtime() uses the time pointed to by t.
tmp = localtime( &t );
// displaying time with strftime
strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp);
printf("The Formatted date & time : %s\n", MY_TIME );
return(0);
}
Output:
The Formatted date &time : 06/03/23 - 05:42PM
Explanation:
All necessary header files (stdlib.h, stdio.h, and time.h) have been included. The character array MY_TIME is allocated based on the Size constant to hold the formatted date and time.
To retrieve the present time in seconds starting from the Unix epoch (January 1, 1970), the time function is employed with the pointer to t as a parameter. This pointer to t is forwarded as a parameter during the invocation of the localtime function. The MY_TIME array holds the structured date and time information.
Utilizing the printf function, the formatted date and time get showcased on the terminal. The main function concludes, and the program exits by returning 0 to indicate successful execution.