The C asctime() function converts the fields of a struct tm into a short, human-readable English string. The layout is always similar to "Sun Jan 7 14:05:09 2024\n".
Pass a valid broken-down time from localtime(), gmtime(), or your own initialized structure. The returned text uses shared static storage, so copy it before another time conversion call if it must be kept. Include <time.h>.
Syntax
Start with this concrete asctime() call:
C
asctime(&when);
Call
What it does
asctime(&when);
Formats the fields in when and returns a pointer to the fixed-layout string.
Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.
Library declaration
C
char *asctime(const struct tm *time_ptr);
Parameters
Parameter
Description
time_ptr
Pointer to a valid struct tm whose fields describe the date and time.
Return Value
Return Value
Description
String pointer
Pointer to a null-terminated, newline-ended string in static storage.
NULL
The conversion could not be represented.
C Examples
Example 1 — Format a Known Date
This example demonstrates asctime() with a small, complete program and checks the relevant result before using it.
Step 1. Build midnight on Saturday, the first day of 2000.
Step 2. Receive the pointer to the library's static character array.
Step 3. The formatted text itself ends with \n.
Step 4. The following words therefore begin on the next output line.
Frequently Asked Questions
asctime() turns a broken-down date and time into a fixed English text line.
Include <time.h> before calling asctime().
It returns a pointer to a fixed English date-time string in static storage. Prefer strftime() for new code.
The result has a fixed English layout and normally contains a trailing newline. Prefer strftime() when you need a chosen format, localization, or a caller-owned output buffer.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.