Overview #
The localtime() function converts a calendar time stored as a time_t value into a broken-down local date and time.
Instead of returning a formatted string like ctime(), localtime() returns a pointer to a struct tm containing individual date and time components such as the year, month, day, hour, minute, and second.
Header File #
#include <time.h>
Man Pages #
man 3 localtime
Function Prototype #
struct tm *localtime(const time_t *timer);
Parameters #
| Parameter | Description |
|---|---|
timer |
Pointer to a time_t object containing the calendar time to be converted. |
Return Value #
On success, localtime() returns a pointer to a struct tm containing the local date and time.
On failure, it returns:
NULL
Data Types #
struct tm #
struct tm stores the individual components of a date and time.
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
| Member | Description |
|---|---|
tm_sec |
Seconds (0โ60) |
tm_min |
Minutes (0โ59) |
tm_hour |
Hours (0โ23) |
tm_mday |
Day of the month (1โ31) |
tm_mon |
Month since January (0โ11) |
tm_year |
Years since 1900 |
tm_wday |
Days since Sunday (0โ6) |
tm_yday |
Days since January 1 (0โ365) |
tm_isdst |
Daylight Saving Time flag |
Examples #
Example 1 โ Display Local Date and Time #
main.c #
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current_time;
struct tm *local;
current_time = time(NULL);
local = localtime(¤t_time);
printf("Date : %02d-%02d-%04d\n",
local->tm_mday,
local->tm_mon + 1,
local->tm_year + 1900);
printf("Time : %02d:%02d:%02d\n",
local->tm_hour,
local->tm_min,
local->tm_sec);
return 0;
}
Output #
Date : 27-07-2026
Time : 15:45:32
The output will be different on your system.
Explanation #
current_time = time(NULL);
Obtains the current calendar time.
local = localtime(¤t_time);
Converts the calendar time into a struct tm representing the local date and time.
local->tm_mon + 1
tm_mon stores months in the range 0โ11, where January is 0. Adding 1 converts it to the familiar range 1โ12.
local->tm_year + 1900
tm_year stores the number of years since 1900. Adding 1900 gives the current year.
Example 2 โ Display Individual Date and Time Components #
main.c #
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current_time;
struct tm *local;
current_time = time(NULL);
local = localtime(¤t_time);
printf("Hour : %d\n", local->tm_hour);
printf("Minute : %d\n", local->tm_min);
printf("Second : %d\n", local->tm_sec);
return 0;
}
Output #
Hour : 15
Minute : 45
Second : 32
The output will be different on your system.
Explanation #
Each member of the struct tm can be accessed using the -> operator because localtime() returns a pointer to the structure.
Notes #
localtime()converts atime_tvalue into local date and time.- It returns a pointer to a
struct tm. tm_monranges from 0 to 11, so add1when displaying the month.tm_yearrepresents the number of years since 1900, so add1900to obtain the actual year.- The returned pointer refers to statically allocated memory managed by the C library.
- Each call to
localtime()may overwrite the result of a previous call. - Do not modify or free the returned structure.
Related Functions #
time()ctime()gmtime()mktime()strftime()