strftime() Function
The strftime()
function in C time.h is used to format date and time information into a human-readable string based on a specified format. It processes a format string containing various format specifiers and produces an output string representing the time details.
Syntax of strftime()
size_t strftime(char *ptr, size_t maxsize, const char *format, const struct tm *timeptr);
Parameters
Parameter | Description |
---|---|
ptr | Pointer to the destination array where the resulting C string is copied. |
maxsize | Maximum number of characters to be copied to ptr , including the terminating null-character. |
format | C string containing a combination of regular characters and format specifiers. |
timeptr | Pointer to a struct tm that contains the broken-down time. |
The function processes the format string, replacing each format specifier (preceded by a %
sign) with the corresponding value from the given time structure. Some specifiers are locale-dependent, and alternative modifiers can be used to request alternative representations.
Specifier Table for strftime()
Specifier | Replaced by | Example |
---|---|---|
%a | Abbreviated weekday name* | Thu |
%A | Full weekday name* | Thursday |
%b | Abbreviated month name* | Aug |
%B | Full month name* | August |
%c | Date and time representation* | Thu Aug 23 14:55:02 2001 |
%C | Year divided by 100 (00-99) | 20 |
%d | Day of the month, zero-padded (01-31) | 23 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 08/23/01 |
%e | Day of the month, space-padded ( 1-31) | 23 |
%F | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2001-08-23 |
%g | Week-based year, last two digits (00-99) | 01 |
%G | Week-based year | 2001 |
%h | Abbreviated month name* (same as %b ) | Aug |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 02 |
%j | Day of the year (001-366) | 235 |
%m | Month as a decimal number (01-12) | 08 |
%M | Minute (00-59) | 55 |
%n | New-line character (‘\n’) | |
%p | AM or PM designation | PM |
%r | 12-hour clock time* | 02:55:02 pm |
%R | 24-hour HH:MM time, equivalent to %H:%M | 14:55 |
%S | Second (00-61) | 02 |
%t | Horizontal-tab character (‘\t’) | |
%T | ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S | 14:55:02 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7) | 4 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%V | ISO 8601 week number (01-53) | 34 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation* | 08/23/01 |
%X | Time representation* | 14:55:02 |
%y | Year, last two digits (00-99) | 01 |
%Y | Year | 2001 |
%z | ISO 8601 offset from UTC (if timezone cannot be determined, no characters are output) | +100 |
%Z | Timezone name or abbreviation (if timezone cannot be determined, no characters are output) | CDT |
%% | A % sign | % |
Return Value
If the length of the resulting C string, including the terminating null-character, does not exceed maxsize
, strftime()
returns the total number of characters copied (excluding the terminating null-character). Otherwise, it returns zero and the contents of the array pointed to by ptr
are indeterminate.
Examples for strftime()
Example 1: Formatting the Current Date and Time Using Locale’s Default Format
This example demonstrates how to obtain the current date and time and format it using the locale’s default date and time representation.
Program
#include <stdio.h>
#include <time.h>
int main() {
char buffer[80];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
/* Format using locale's default date and time representation */
strftime(buffer, sizeof(buffer), "%c", timeinfo);
printf("Current Date and Time: %s\n", buffer);
return 0;
}
Explanation:
- The program retrieves the current time using
time()
and converts it to local time withlocaltime()
. strftime()
is used with the%c
specifier to format the date and time according to the locale’s default representation.- The formatted string is stored in
buffer
and then printed.
Program Output:
Current Date and Time: Sun Feb 23 04:39:15 2025
Example 2: Custom Date Format (YYYY-MM-DD)
This example shows how to format the date in the ISO 8601 short format using the %F
specifier.
Program
#include <stdio.h>
#include <time.h>
int main() {
char buffer[20];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
/* Format date as YYYY-MM-DD */
strftime(buffer, sizeof(buffer), "%F", timeinfo);
printf("Formatted Date: %s\n", buffer);
return 0;
}
Explanation:
- The current time is fetched and converted to local time.
- The
%F
specifier formats the date as YYYY-MM-DD. - The resulting date string is stored in
buffer
and printed.
Program Output:
Formatted Date: 2025-02-23
Example 3: Detailed Time Format with Multiple Specifiers
This example demonstrates how to create a detailed formatted string using multiple specifiers to display various components of the date and time.
Program
#include <stdio.h>
#include <time.h>
int main() {
char buffer[100];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
/* Format: Full weekday name, day, full month name, year, and time in HH:MM:SS */
strftime(buffer, sizeof(buffer), "%A, %d %B %Y - %T", timeinfo);
printf("Detailed Time Format: %s\n", buffer);
return 0;
}
Explanation:
- The current time is retrieved and converted to a local time structure.
- The format string uses multiple specifiers:
%A
for full weekday name,%d
for day of the month,%B
for full month name,%Y
for year, and%T
for time in HH:MM:SS format. strftime()
combines these into a detailed formatted string.- The final string is stored in
buffer
and then printed.
Program Output:
Detailed Time Format: Sunday, 23 February 2025 - 04:39:31