strftime() function in C/C++


strftime() is a function in C which is used to format date and time. It comes under the header file time.h, which also contains a structure named struct tm which is used to hold the time and date. The syntax of strftime() is as shown below :

size_t strftime(char *s, size_t max, const char *format, 
                                          const struct tm *tm); 

strftime() function formats the broken-down time tm according to the formatting rules specified in format and store it in character array s.

Some format specifiers for strftime() are shown as follows :
%x = Preferred date representation
%I = Hour as a decimal number (12-hour clock).
%M = Minutes in decimal ranging from 00 to 59.
%p = Either “AM” or “PM” according to the given time value, etc.
%a = Abbreviated weekday name
%A = Full weekday name
%b = Abbreviated month name
%B = Full month name March
%c = Date and time representation
%d = Day of the month (01-31)
%H = Hour in 24h format (00-23)
%I = Hour in 12h format (01-12)
%j = Day of the year (001-366)
%m = Month as a decimal number (01-12)
%M = Minute (00-59)

Structure struct tm is defined in time.h as follows :

struct tm 
   int tm_sec;         // seconds
   int tm_min;         // minutes
   int tm_hour;        // hours
   int tm_mday;        // day of the month
   int tm_mon;         // month
   int tm_year;        // The number of years since 1900
   int tm_wday;        // day of the week
   int tm_yday;        // day in the year
   int tm_isdst;       // daylight saving time	
// C program to demonstrate the 
// working of strftime()
#include <stdlib.h>
#include <stdio.h>
#include <time.h> 
#define Size 50

int main ()
    time_t t ;
    struct tm *tmp ;
    char MY_TIME[Size];
    time( &t );
    //localtime() uses the time pointed by t ,
    // to fill a tm structure with the 
    // values that represent the 
    // corresponding local time.
    tmp = localtime( &t );
    // using strftime to display time
    strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp);
    printf("Formatted date & time : %s\n", MY_TIME );

Formatted date & time : 03/20/17 - 02:55PM

Why and when do we use strftime() ?

When we are making a software/application which will output the current time and most important in many different formats on the user’s demand. Then in that case we will use this function. Its specialty is that we can display date and time in many different formats.

Reference:>Linux Man Page

This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at to report any issue with the above content.

Recommended Posts:

4.2 Average Difficulty : 4.2/5.0
Based on 9 vote(s)

Writing code in comment? Please use, generate link and share the link here.