Tutorials  Articles  Notifications  Login  Signup


VS

Vishal Saxena

SDE 1 at Amazon Updated Dec. 22, 2019, 6:12 p.m. ⋅ 1201 views

time.h localtime() function in C


The localtime() function is defined in the time.h header file. The localtime( ) function return the local time of the user i.e time present at the task bar in computer.

Here is how it's syntax looks:

tm* localtime(const time_t* t_ptr);

localtime() function accepts a parameter t_ptr which represents the pointer to time_t object and it returns a pointer to a struct tm object.

Here is a program to implement it.

 

// C program to demonstrate 
// example of localtime() function. 

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

int main() 
{ 

	struct tm* local; 
	time_t t = time(NULL); 

	// Get the localtime 
	local = localtime(&t); 

	printf("Local time and date is: %s\n", 
		asctime(local)); 

	return 0; 
}
Local time and date is: Sun Dec 22 18:10:53 2019

 

If you'll change the time of your local computer, it will change.



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account