strftime() function in C/C++ Last Updated : 16 Apr, 2021 Comments Improve Suggest changes Like Article Like 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 Comment More infoAdvertise with us Next Article strftime() function in C/C++ M MAZHAR IMAM KHAN Improve Article Tags : C Language C++ CPP-Library Practice Tags : CPP Similar Reads localtime() function in C++ The localtime() function is defined in the ctime header file. The localtime() function converts the given time since epoch to calendar time which is expressed as local time. Syntax: tm* localtime(const time_t* time_ptr); Parameter: This function accepts a parameter time_ptr which represents the poin 1 min read mktime() function in C++ STL The mktime() is an inbuilt C++ function which converts the local calendar time to the time since epoch and returns the value as an object of type time_t. Syntax : time_t mktime( struct tm *time_ptr ) Parameters: The function accepts a mandatory parameter pointer time_ptr that points to a tm object s 2 min read Measure execution time of a function in C++ We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we 3 min read clock() function in C The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be 2 min read gmtime() Function in C The gmtime() function in C takes a pointer to type t_time value which represents type in seconds and converts it to struct tm. In this article, we will learn gmtime() Function in the C programming language. The struct tm type can hold the individual components of time in UTC(Universal Time Coordinat 2 min read asctime() function in C++ The asctime() function is defined in the ctime header file. The asctime() function converts the given calendar time of structure tm to a character representation i.e human readable form. Syntax: char* asctime(const struct tm * time_ptr); Parameter: This function accepts single parameter time_ptr i.e 1 min read Print system time in C++ (3 different ways) First Method Printing current date and time using time() Second Method CPP // CPP program to print current date and time // using time and ctime. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { // declaring argument of time() time_t my_time = time(NULL); // ct 2 min read Date and Time Parsing in C++ The Date and time parsing is a common task in programming especially when dealing with user input or data from external sources. C++ provides various utilities and libraries to handle date and time parsing efficiently. Some of the most commonly used libraries for date and time parsing in C++ are: 5 min read C Library Function - difftime() The difftime() is a C Library function that returns the difference in time, in seconds(i.e. ending time - starting time). It takes two parameters of type time_t and computes the time difference in seconds. The difftime() function is defined inside the <time.h> header file. Syntax The syntax of 1 min read getdate() and setdate() function in C with Examples setdate() Method: getdate() function is defined in dos.h header file. This function fills the date structure *dt with the system's current date. Syntax struct date dt; getdate(&dt); Parameter: This function accepts a single parameter dt which is the object of structure date. Return Value: This 2 min read Like