C Date and Time

Beginner
8 min read
Updated: Nov 2026
1 example
<time.h>

The C Time Model

C separates an instant, calendar fields, processor usage, and display text. The <time.h> header supplies types and functions that move between these forms instead of forcing one date representation on every system.

A common workflow is: call time() to get the current instant, call localtime() or gmtime() to obtain named fields, then call strftime() to create readable text. Every stage has its own return check.

Key idea.

A time_t is suitable for passing an instant through the C library. Do not assume its epoch, signedness, width, or exact unit when portable code can use library functions.

Core Types in time.h

TypePurposeFirst API to learn
time_tCalendar-time value representing an instant.time()
struct tmBroken-down year, month, day, and clock fields.localtime()
clock_tImplementation-defined processor-time count.clock()
struct timespecWhole seconds and a nanosecond fraction.timespec_get()

Keep values in their declared types. Convert to a display type only at the final print boundary when necessary, and never use an integer cast as a replacement for the library's conversion rules.

Calendar Time vs Processor Time

QuestionCalendar timeProcessor time
What does it describe?An instant or elapsed real-world interval.CPU time consumed by the program.
Main typetime_t or struct timespecclock_t
Main functiontime() or timespec_get()clock()
Conversiondifftime(end, start)(end-start) / CLOCKS_PER_SEC
Does sleeping count?Normally yes for elapsed calendar time.Often little or not at all.

Choose based on the question. Benchmark CPU work with clock(); timestamp an event or compare real instants with calendar-time functions.

Understanding struct tm

struct tm gives names to calendar components, but several fields use library-friendly ranges rather than everyday notation.

FieldMeaningConvention
tm_yearYearYears since 1900
tm_monMonth0 through 11
tm_mdayDay of month1 through 31
tm_hourHour0 through 23
tm_min, tm_secMinute and secondNormally start at 0
tm_wdayWeekdaySunday is 0
tm_ydayDay of yearJanuary 1 is 0
tm_isdstDaylight-saving statusUse -1 to ask mktime() to determine it

Starter: Print the Current Local Time

This complete program follows the standard three-stage workflow and checks every operation.

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

int main(void) {
    time_t now = time(NULL);
    if (now == (time_t)-1) {
        printf("Calendar time is unavailable.\n");
        return 1;
    }

    struct tm *local = localtime(&now);
    if (local == NULL) {
        printf("Local conversion failed.\n");
        return 1;
    }

    char text[64];
    if (strftime(text, sizeof text, "%Y-%m-%d %H:%M:%S", local) == 0) {
        printf("Formatting failed.\n");
        return 1;
    }

    printf("Local time: %s\n", text);
    return 0;
}

How It Works

  1. Read an instant. time(NULL) returns current calendar time, and the cast -1 check catches failure.
  2. Apply local rules. localtime() converts that instant into local year, month, day, and clock fields.
  3. Choose the text layout. strftime() expands numeric directives into a caller-owned buffer and reports whether they fit.
  4. Print validated text. The final printf() runs only after acquisition, conversion, and formatting all succeed.

Three Functions to Learn First

Frequently Asked Questions

time_t is an implementation-defined arithmetic type used by the C library to represent calendar time.
struct tm is a broken-down calendar representation with named year, month, day, hour, minute, second, weekday, and related fields.
No. clock() reports processor time used by the program. It is not the current date and may barely change while the program sleeps.
Call time(), convert with localtime() or gmtime(), and format with strftime(). Check each result.
Start with time(), localtime(), strftime(), difftime(), and clock().

Browse time.h functions

Choose the right API for measurement, conversion, or formatting.

Function index →

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.

10 people found this page helpful