C Program to demonstrate time functions.

The time.h library in C provides functions for getting the current date and time, formatting it for display, and converting between representations. The central type is time_t — typically a 32-bit or 64-bit integer counting seconds since the Unix epoch (1970-01-01 00:00:00 UTC). Five key functions cover the most common time operations: time(), ctime(), localtime(), asctime(), and strftime().

time.h Functions at a Glance

Function Input Output Use case
time(NULL) nothing time_t (epoch seconds) Get current time as a number
ctime(&t) time_t * char * with newline Quick human-readable timestamp
localtime(&t) time_t * struct tm * Get year/month/day/hour/min/sec separately
asctime(tm) struct tm * char * with newline Format a struct tm like ctime
strftime(buf, n, fmt, tm) struct tm * written to buf Custom date/time format string

C Program for Time Functions

/* time.h functions: time(), ctime(), localtime(), asctime(), strftime()
 * Compile: gcc -ansi -Wall -Wextra timefuncs.c -o timefuncs */
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t now;
    struct tm *local;
    char buf[80];

    /* time() returns seconds since the Unix epoch (1970-01-01 00:00:00 UTC) */
    now = time(NULL);
    printf("Seconds since epoch : %ld\n\n", (long)now);

    /* ctime() formats the time_t value as a human-readable string */
    printf("ctime()             : %s", ctime(&now));  /* ctime adds '\n' */

    /* localtime() converts time_t to a broken-down local-time struct tm */
    local = localtime(&now);
    printf("localtime fields:\n");
    printf("  Year : %d\n", local->tm_year + 1900);  /* years since 1900 */
    printf("  Month: %d\n", local->tm_mon + 1);       /* 0-based, so +1 */
    printf("  Day  : %d\n", local->tm_mday);
    printf("  Hour : %d\n", local->tm_hour);
    printf("  Min  : %d\n", local->tm_min);
    printf("  Sec  : %d\n\n", local->tm_sec);

    /* asctime() formats a struct tm to a string (same format as ctime) */
    printf("asctime()           : %s", asctime(local));

    /* strftime() formats with full control over the output string */
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", local);
    printf("strftime ISO 8601   : %s\n", buf);

    strftime(buf, sizeof(buf), "%A, %B %d, %Y", local);
    printf("strftime verbose    : %s\n", buf);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra timefuncs.c -o timefuncs
./timefuncs

Sample Output

Seconds since epoch : 1782751735

ctime()             : Mon Jun 29 16:48:55 2026
localtime fields:
  Year : 2026
  Month: 6
  Day  : 29
  Hour : 16
  Min  : 48
  Sec  : 55

asctime()           : Mon Jun 29 16:48:55 2026
strftime ISO 8601   : 2026-06-29 16:48:55
strftime verbose    : Monday, June 29, 2026

struct tm Fields Reference

Field Type Range Note
tm_year int years since 1900 Add 1900 to get the calendar year
tm_mon int 0–11 0=January, 11=December — add 1 for human display
tm_mday int 1–31 Day of month (1-based, unlike tm_mon)
tm_hour int 0–23 24-hour format
tm_min int 0–59 Minutes
tm_sec int 0–60 60 allows for a leap second
tm_wday int 0–6 0=Sunday, 6=Saturday
tm_yday int 0–365 Day of year (0=Jan 1)
tm_isdst int positive/0/−1 Daylight saving: >0=yes, 0=no, −1=unknown

strftime Format Codes

Code Example Description
%Y 2026 4-digit year
%m 06 Month 01–12
%d 29 Day 01–31
%H 16 Hour 00–23
%M 48 Minute 00–59
%S 55 Second 00–60
%A Monday Full weekday name
%B June Full month name
%I 04 12-hour clock hour
%p PM AM or PM

Code Explanation

  • time(NULL) — passes NULL so the function returns the current time without storing it into a pointer. The return value (a time_t) is assigned to now. You can also write time(&now) which both sets now and returns its value.
  • (long)now for printf — time_t might be int or long depending on the platform. Casting to long and using %ld is safe on both 32-bit and 64-bit systems.
  • ctime() includes the newline — ctime() and asctime() both append ‘\n’ to their output string. If you use printf("%s", ctime(&now)), do not add another ‘\n’ in the format string or you will get a blank line.
  • tm_year + 1900 — the struct tm year field counts years since 1900. For 2026: tm_year = 126, and 126 + 1900 = 2026. This is one of the most common off-by-1900 bugs in C time handling.
  • tm_mon + 1 — months are 0-based (0=January). Add 1 for human display. tm_mday is 1-based (no offset needed).
  • strftime(buf, sizeof(buf), fmt, local) — the sizeof(buf) argument limits the output length to prevent buffer overflow. strftime returns the number of characters written (excluding null terminator), or 0 if the buffer is too small.

What This Program Teaches

  • time_t is an integer — the Unix epoch representation (seconds since 1970-01-01) is just a number. You can subtract two time_t values to get the elapsed seconds between two moments.
  • struct tm broken-down time — use localtime() when you need to access or manipulate individual date/time components (display day of week, check if after 5pm, etc.). Use ctime() or strftime() when you just need a formatted string.
  • strftime for custom formats — strftime gives you full control over how dates are displayed. The ISO 8601 format (2026-06-29) is internationally unambiguous — use it when building log files, file names, or databases.
  • localtime vs gmtime — localtime() converts to the local timezone (set by the TZ environment variable or system setting). gmtime() converts to UTC. For timestamps in log files shared across timezones, use gmtime.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>