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 ); 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
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call function within function in C or C++
- Difference between virtual function and inline function in C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Importance of function prototype in C
- What happens when a function is called before its declaration in C?
- Default arguments and virtual function

