C asctime() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

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);
CallWhat 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

ParameterDescription
time_ptrPointer to a valid struct tm whose fields describe the date and time.

Return Value

Return ValueDescription
String pointerPointer to a null-terminated, newline-ended string in static storage.
NULLThe 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.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    struct tm when = {0};
    when.tm_year = 124;
    when.tm_mon = 0;
    when.tm_mday = 7;
    when.tm_hour = 14;
    when.tm_min = 5;
    when.tm_sec = 9;
    printf("%s", asctime(&when));
    return 0;
}

How It Works

  1. Step 1. Zero-initialize the structure so unused fields have predictable values.
  2. Step 2. Store 2024 as years since 1900 and January as month 0.
  3. Step 3. Set the clock fields to 14:05:09.
  4. Step 4. asctime() creates the fixed-width line and printf() uses its built-in newline.

Example 2 — Use a Local Time

This example demonstrates asctime() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t stamp = (time_t)1704636309;
    struct tm *local = localtime(&stamp);
    if (local != NULL) {
        printf("%s", asctime(local));
    }
    return 0;
}

How It Works

  1. Step 1. Create a sample calendar-time value.
  2. Step 2. localtime() breaks it into local calendar fields.
  3. Step 3. Check the returned pointer before using it.
  4. Step 4. asctime() formats those local fields in its fixed English layout.

Example 3 — Inspect the Trailing Newline

This example demonstrates asctime() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    struct tm when = {0};
    when.tm_year = 100;
    when.tm_mday = 1;
    when.tm_wday = 6;
    char *text = asctime(&when);
    if (text != NULL) {
        printf("date: %snext line\n", text);
    }
    return 0;
}

How It Works

  1. Step 1. Build midnight on Saturday, the first day of 2000.
  2. Step 2. Receive the pointer to the library's static character array.
  3. Step 3. The formatted text itself ends with \n.
  4. 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.

Next: clock()

Continue through the C time.h function tutorials.

clock() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful