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()

</>
Copy
size_t strftime(char *ptr, size_t maxsize, const char *format, const struct tm *timeptr);

Parameters

ParameterDescription
ptrPointer to the destination array where the resulting C string is copied.
maxsizeMaximum number of characters to be copied to ptr, including the terminating null-character.
formatC string containing a combination of regular characters and format specifiers.
timeptrPointer 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()

SpecifierReplaced byExample
%aAbbreviated weekday name*Thu
%AFull weekday name*Thursday
%bAbbreviated month name*Aug
%BFull month name*August
%cDate and time representation*Thu Aug 23 14:55:02 2001
%CYear divided by 100 (00-99)20
%dDay of the month, zero-padded (01-31)23
%DShort MM/DD/YY date, equivalent to %m/%d/%y08/23/01
%eDay of the month, space-padded ( 1-31)23
%FShort YYYY-MM-DD date, equivalent to %Y-%m-%d2001-08-23
%gWeek-based year, last two digits (00-99)01
%GWeek-based year2001
%hAbbreviated month name* (same as %b)Aug
%HHour in 24h format (00-23)14
%IHour in 12h format (01-12)02
%jDay of the year (001-366)235
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%nNew-line character (‘\n’)
%pAM or PM designationPM
%r12-hour clock time*02:55:02 pm
%R24-hour HH:MM time, equivalent to %H:%M14:55
%SSecond (00-61)02
%tHorizontal-tab character (‘\t’)
%TISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S14:55:02
%uISO 8601 weekday as number with Monday as 1 (1-7)4
%UWeek number with the first Sunday as the first day of week one (00-53)33
%VISO 8601 week number (01-53)34
%wWeekday as a decimal number with Sunday as 0 (0-6)4
%WWeek number with the first Monday as the first day of week one (00-53)34
%xDate representation*08/23/01
%XTime representation*14:55:02
%yYear, last two digits (00-99)01
%YYear2001
%zISO 8601 offset from UTC (if timezone cannot be determined, no characters are output)+100
%ZTimezone 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

</>
Copy
#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:

  1. The program retrieves the current time using time() and converts it to local time with localtime().
  2. strftime() is used with the %c specifier to format the date and time according to the locale’s default representation.
  3. 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

</>
Copy
#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:

  1. The current time is fetched and converted to local time.
  2. The %F specifier formats the date as YYYY-MM-DD.
  3. 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

</>
Copy
#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:

  1. The current time is retrieved and converted to a local time structure.
  2. 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.
  3. strftime() combines these into a detailed formatted string.
  4. The final string is stored in buffer and then printed.

Program Output:

Detailed Time Format: Sunday, 23 February 2025 - 04:39:31