strftime() / asctime() / ctime()
Functions for converting a time value to a string. Defined in <time.h>, they are widely used for writing timestamps to log files and displaying date/time information.
Syntax
// Converts a struct tm to a string according to a format string. // Returns: the number of characters written (excluding the null terminator '\0'), or 0 if the buffer is too small. size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *tm); // Converts a struct tm to a fixed-format English string (not thread-safe). // Returns: a pointer to a static string in the format "Www Mmm dd hh:mm:ss yyyy\n". char *asctime(const struct tm *tm); // Converts a time_t to a fixed-format English string (not thread-safe). // Returns: a pointer to a string equivalent to asctime(localtime(timer)). char *ctime(const time_t *timer);
Common strftime Format Specifiers
| Specifier | Description | Example |
|---|---|---|
| %Y | Four-digit year. | 2024 |
| %m | Two-digit month (01–12). | 03 |
| %d | Two-digit day of the month (01–31). | 15 |
| %H | Two-digit hour (00–23, 24-hour clock). | 14 |
| %M | Two-digit minute (00–59). | 30 |
| %S | Two-digit second (00–60). | 05 |
| %A | Full weekday name (locale-dependent). | Friday |
| %a | Abbreviated weekday name (locale-dependent). | Fri |
| %B | Full month name (locale-dependent). | March |
| %b | Abbreviated month name (locale-dependent). | Mar |
| %p | AM or PM (locale-dependent). | PM |
| %Z | Timezone name. | JST |
| %% | A literal % character. | % |
Sample Code
#include <stdio.h>
#include <time.h>
int main(void) {
time_t now = time(NULL);
struct tm *local = localtime(&now);
// Use strftime to format the time in any format you like.
char buf[128];
// Output in ISO 8601 format (international standard, suitable for logs).
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", local);
printf("ISO 8601: %s\n", buf); // e.g., "2024-03-15T10:30:05"
// Format suitable for filenames (avoids spaces and colons).
strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", local);
printf("Filename: %s\n", buf); // e.g., "20240315_103005"
// Use asctime to convert to a fixed English format.
printf("asctime: %s", asctime(local)); // The string already ends with '\n'.
// e.g., "Fri Mar 15 10:30:05 2024"
// Use ctime to convert a time_t directly to a string (equivalent to localtime + asctime).
printf("ctime: %s", ctime(&now));
// e.g., "Fri Mar 15 10:30:05 2024"
return 0;
}
Notes
strftime() is the most flexible and practical of the three functions, as it lets you specify any format using format specifiers. For logging timestamps, the ISO 8601 format (%Y-%m-%dT%H:%M:%S) is widely used internationally.
%A (weekday name) and %B (month name) are locale-dependent and may output in languages other than English depending on the system locale. For portability, consider using a fixed array instead (e.g., weekdays[] = {"Sun", "Mon", ...}).
asctime() and ctime() are not thread-safe. They return a pointer to an internal static buffer, so calling them from multiple threads simultaneously can cause a data race. Also note that the string returned by asctime() already includes a trailing newline ('\n'), so avoid adding an extra newline when passing it to printf().
For information on obtaining a struct tm, see localtime() / gmtime() / mktime(). For obtaining a time_t, see time() / difftime() / clock().
If you find any errors or copyright issues, please contact us.