C++ asctime()

The asctime() function is defined in <ctime> header file.

asctime() prototype

char* asctime(const struct tm * time_ptr);

The asctime() function takes a pointer to tm object as its parameter and returns a text representation for a given calendar time of the form:

Www Mmm dd hh:mm:ss yyyy
asctime() representation

Type

Description

Obtained from

Values

Www

3 letter day of week

time_ptr->tm_wday

Mon to Sun

Mmm

3 letter month name

time_ptr->tm_mon

Jan to Dec

dd

2 digit day of month

time_ptr->tm_mday

00 to 31

hh

2 digit hour

time_ptr->tm_hour

00 to 23

mm

2 digit minute

time_ptr->tm_min

00 to 59

ss

2 digit second

time_ptr->tm_sec

00 to 59

yyyy

4 digit year

time_ptr->tm_year + 1900

4 digit number


asctime() Parameters

  • time_ptr: pointer to a tm object to be converted.

asctime() Return value

  • Pointer to a null terminated string the points to the character representation of the given time.

Example: How asctime() function works?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t curr_time;

	time(&curr_time);
	cout << "Current date and time: " << asctime(localtime(&curr_time));

	return 0;
}

When you run the program, the output will be:

Current date and time: Tue Mar 21 13:52:57 2017