Table of contents
1.
Introduction
2.
Parameter and Return Type of asctime()
3.
Example on asctime() Function
4.
Frequently Asked Questions
4.1.
What is asctime() function in c++?
4.2.
What is the use of the time() function in c++?
4.3.
What is the use of the localtime() function in c++?
4.4.
What is the tm struct?
4.5.
What is asctime_s() function?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

asctime() Function in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about the asctime() function of c++ to get the calendar time. The ctime header file defines the asctime() function. The asctime() method transforms a calendar time of structure tm to a character representation or human-readable form.

Header File:

#include <ctime>


Syntax:

char *asctime(const struct tm *time_ptr);

Explanation: The asctime() function written above takes a pointer of the tm object and returns the given time object in the form of Www Mmm dd hh:mm:ss yyyy.

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

Parameter and Return Type of asctime()

This section will discuss the arguments that the asctime() function takes and the return value.

Parameter:

According to the syntax written above, it takes the pointer object "time_ptr" which is of type tm.

Return Type:

A pointer points to the character representation of the supplied time to a null-terminated string.

This function returns the result as Www Mmm dd hh:mm:ss yyyy. Let's discuss this form more extensively.

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 ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass