Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Syntax
1.2.
Parameter
1.3.
Return Value
2.
CODE
3.
Output
4.
FAQs
4.1.
What is a localtime() function?
4.2.
In which header file, localtime() function is there?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

localtime() function in C++

Author Nishant Rana
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C++ is a powerful programming language for your competitive programming. To get flawless control in this language, you can refer to this amazing article on Coding Ninjas StudioC++ STL consists of a lot of inbuilt functions with different functionalities. The localtime() function is one of those functions. The localtime() function is used to convert the given time into the local time. It is present in the <ctime> header file and we can use it by importing this header file.

Also see, Literals in C, Fibonacci Series in C++

Syntax

localtime(time_t* timer);

Parameter

It accepts a timer value which is a pointer to the time_t object.

Return Value

The localtime() function returns a pointer pointing to the structure of the time which is stored in the local timezone.

CODE

#include <bits/stdc++.h>
using namespace std;
int main(){
   time_t hold;
   hold = time(NULL);
   tm* hold_local = localtime(&hold);
   cout<<"Current local time of system is: "<< hold_local->tm_hour << ":"<<hold_local->tm_min << ":"<<hold_local->tm_sec;
   return 0;
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Output

Current local time of system is: 18:7:37

 

Try and compile with online c++ compiler.

FAQs

What is a localtime() function?

The localtime() function is used to convert the given time into the local time.

In which header file, localtime() function is there?

It is present in the <ctime> header file and we can use it by importing this header file.

Conclusion

In this article, we have extensively discussed the localtime() function in C++. We start with a brief introduction to C++, then discuss the swap() function and its examples.

After reading this blog, you will be able to use localtime() function in C++, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see Basics of C++C++.

Recommended Readings:

 

You can refer to the Data Structures and AlgorithmsJavaScript courses which can upskill you. You can discover more such courses by referring to the Guided Path on Coding Ninjas Studio. You can also test your knowledge by taking up the mock test and contests which are held by Code studio.

If you are about to interview with companies like Google, Adobe, Amazon, etc. then you can refer to problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass