Open In App

strftime() function in C/C++

Improve
Improve
Like Article
Like
Save
Share
Report

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 = Abbreviated weekday name in capital letters
%A = Full weekday name 
%b = Abbreviated month name 

%^b = Abbreviated month name in capital letters
%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




// 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 );
    return(0);
}


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: http://man7.org/linux/man-pages/man3/strftime.3.html>Linux Man Page

 


Last Updated : 16 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads