Example on asctime() Function
This section will clearly discuss a simple example to understand the asctime() function.
Code:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//Creating a time_t object.
time_t my_time;
//This function returns the time since 00:00:00 UTC.
time(&my_time);
//Passing the local time to the asctime function.
cout << "My current date and time: " << asctime(localtime(&my_time));
return 0;
}
Output:

Explanation: Returning the result in the form of Www Mmm dd hh:mm:ss yyyy format.
Try and compile with online c++ compiler.
Frequently Asked Questions
What is asctime() function in c++?
The specified calendar time of the tm structure is converted to a character representation using the asctime() method in C++. The ctime header file contains a definition for the asctime() method.
What is the use of the time() function in c++?
The time() function is specified in the header file time. h (ctime in C++). This function calculates the time in seconds since 00:00:00 UTC on January 1, 1970 (Unix timestamp). The returned value is also kept in the object pointed to by the second if the second is not a null pointer.
What is the use of the localtime() function in c++?
The localtime() function accepts a time t pointer as a parameter and returns a pointer object with the structure tm. The local time is returned by the localtime() method. The hours, minutes, and seconds can then be obtained using the functions tm hour, tm min, and tm sec.
What is the tm struct?
In the C programming language, struct tm is a structure specified in the time.h header file. The time and date recorded on a machine are stored in each member object of struct tm.
What is asctime_s() function?
The calendar time that has been provided can be represented textually using this function. In contrast to asctime_s(), which allows us to change the calendar time, asctime() does not allow us to change the output calendar time.
Conclusion
In this article, we have extensively discussed the asctime() function in c++. We discussed its parameter, return type and return format.
Recommended Readings:
We hope this blog has helped you enhance your knowledge regarding asctime(). Check out the awesome content on the Coding Ninjas Website Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.
Happy Coding!