C Programming

Strftime() C Function

The “strftime()” function in the C programming is utilized to format the date and the time. To utilize this function, we must include the “time. h” header file. This also has a component called struct “tm” that stores the time and date. When we want to format the date and the time, we use this “strftime()” function in the C programming. The “strftime()” function applies the formatting guidelines provided in a format to the broken-down time “tm” before storing it in a character array. In this guide, we will provide a deep detail of this “strftime()” function and will explore the examples herein where we utilize this “strftime()” function.

Syntax:

Format Specifier for Strftime() Function in C

  • %x for representing the date
  • %| representing hours (as a decimal number)
  • %M representing minutes (in decimal)
  • %p either for “am” or “pm”
  • %a weekend day name (abbreviated)
  • %^a capital weekend day name (abbreviated)
  • %A weekend name (full)
  • %b month name (abbreviated)
  • %^b capital month name
  • %d representing month days (01-31)
  • %H representing hours (24 hours format)
  • %I am representing Hour (12 hours format)
  • %j representing days of the year
  • %M representing minute

Example #1:

The examples in this guide will carry out using the Ubuntu 20.04. Thus, the GCC compiler needs to be installed to perform these examples using the Ubuntu 20.04. When GCC is installed, a little amount of the C code is written to show how to utilize the “strftime()” function. We utilize some header files at the start of the code. We have a “stdlib” header file which we utilize for the different functions such as memory allocation, process management, etc. Then, we have the “stdio. h” header file. This header file is the standard input/output header file. We also utilize the “time. h” as we have to use the “strftime()” function toformat the date and time. Then, we have the “define” directive and use the “size” as “50”.

We utilize the “main” method here and it is the “int” data type. Inside the main() function, we declare a variable named “t” of the “time_t” data type. The “tmp” is declared here as the “struct tm”. Then, we have a “char” array of the name “MY_TIME [size]” and pass the “Size” here. We use the time() function and pass the address of the “t” variable as “&t”.

Then, we initialize the “tmp” with the “localtime()” function and pass the “&t” here. This localtime function is used to return the time of the computer. Now, we have the “strftime()” function which is used here to display the time. In this “strftime()” function, we use the “%x” to display the date, “%I” to display the hours, and “%M” to display the minutes. The “printf” statement renders the line on the terminal. Then, we have the “return 0” statement.

We utilize the GCC commands here to get the output. The first command is used to compile the code. After compilation, we have the second command to render the output. Here, you can observe the formatted date and time on the terminal screen.

Example #2:

In this example, we change the code. We utilize the two header files here, the “stdio. h” and the “time. h” header file. We explained both header files in the previous example. We have the “main()” function of the “int” data type. Then, below this, we declare the variable named “anytime” for the “time_t” and the “now” for the “struct tm”. We use the “time_string” of the size “64” and use the “char” data type for this. The address of the “anytime” variable is passed as a parameter to the time() function as “&anytime”. The “localtime()” function is used to initialize the “now” after which the “&anytime” is passed.

The localtime function is used to retrieve the computer’s time. The localtime() uses the time pointed to by anytime to populate a tm structure with the data that represent the current local time. The “strftime()” function is now utilized to display the time in this situation. The “strftime()” function is used here and we have the“%A” which represents the full weekend name, the “%B” represents the full month name, and the “%d” is to represent the day. The “%Y” is used to represent the year. Now, we want to display this day, month, date, and year on the terminal so we utilize the “printf” statement which renders all of them on the screen.

In this output, we again utilize both commands of the GCC compiler so it provides the desired output on the screen. We compile the code first, it is error-free, so we type the second command and the output is rendered in the following.  Look at the output in the following screenshot. You can see the full day name, full month name, date, and year on the screen. We get all these by utilizing the “strftime()” function in the C code.

Conclusion

This guide’s main goal is to help you understand how the “strftime()” C programming function is utilized. This guide covered the “strftime()” function’s function, codes, and outcomes. The “strftime()” C function has previously been covered in this guide. As we mentioned here, the “strftime()” function in C is used to format the date, day, and time. We looked at the two examples here. The output from each instructional example is provided, along with the C source codes required to use the “strftime()” function. We are assured that after carefully reading this guide and performing those codes by yourself, you will comprehend this “strftime()” function easily.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.