Skip to content
Related Articles

Related Articles

Improve Article

localtime() function in C++

  • Last Updated : 01 Oct, 2018

The localtime() function is defined in the ctime header file. The localtime() function converts the given time since epoch to calendar time which is expressed as local time.


tm* localtime(const time_t* time_ptr);

Parameter: This function accepts a parameter time_ptr which represents the pointer to time_t object.

Return Value: This function returns a pointer to a tm object on success, Otherwise it returns NullPointerException.

Below program illustrate the localtime() function in C++:


// c++ program to demonstrate
// example of localtime() function.
#include <bits/stdc++.h>
using namespace std;
int main()
    time_t time_ptr;
    time_ptr = time(NULL);
    // Get the localtime
    tm* tm_local = localtime(&time_ptr);
    cout << "Current local time is = "
         << tm_local->tm_hour << ":"
         << tm_local->tm_min << ":"
         << tm_local->tm_sec;
    return 0;
Current local time is = 10:8:10
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :