Strftime() in C
The time.h header file contains a definition for the strftime function. Its purpose is to generate and save a string in the specified format. It makes use of the time values kept in a specific tm struct.
The syntax for the strftime() method is provided below.
The parameters or arguments used:
It is a pointer to the target array where the copied C string from the result is stored.
It is the maximum character count that can be copied to str.
It is the C string that can contain any mixture of alphabetic and numeric characters as well as unique format specifiers. The function replaces these format specifiers with the correct values to represent the time supplied in tm.
These are the format specifiers:
The timeptr -
It is a pointer to a tm structure that holds the components of a calendar time, as seen below.
The total number of characters copied to str (excluding the ending null-character) is returned if the resulting C string can be written in fewer than size characters (which includes the terminating null-character), in which case zero is returned.
The strftime() method is used in the example below.
The Formatted date & time is : |06/03/23 - 05:17PM|
The Formatted date &time : 06/03/23 - 05:42PM
Here all the essential header files (stdlib.h, stdio.h, and time.h) are added. The size of the character array MY_TIME is defined by the Size constant, which will store the formatted date and time.
To obtain the current time in seconds since the Unix epoch (January 1, 1970), the time() function is used with the address of t as an argument. The address of t is passed as an argument when the localtime() method is called. The MY_TIME array contains the formatted date and time.
Using printf(), the formatted date and time are displayed on the terminal. The main() function comes to a close, and the program returns 0 to signify that it ran successfully.