C++ ctime()

The ctime() function in C++ converts the given time since epoch to a calendar local time and then to a character representation.

A call to ctime(time) is a combination of asctime() and localtime() functions, as asctime(localtime(time)).

It is defined in <ctime> header file.

ctime() prototype

char* ctime(const time_t* time_ptr);

The ctime() function takes a pointer to time_t object as its parameter and returns a text representation of the form:

Www Mmm dd hh:mm:ss yyyy
ctime() time representation

Type

Description

Values

Www

3 letter day of week

Mon to Sun

Mmm

3 letter month name

Jan to Dec

dd

2 digit day of month

00 to 31

hh

2 digit hour

00 to 23

mm

2 digit minute

00 to 59

ss

2 digit second

00 to 59

yyyy

4 digit year

4 digit year


ctime() Parameters

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

ctime() Return value

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

Example: How ctime() function works?

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

int main()
{
	time_t curr_time;
	curr_time = time(NULL);

	char *tm = ctime(&curr_time);
	cout << "Today is : " << tm;
	
	return 0;
}

When you run the program, the output will be:

Today is : Fri Mar 24 18:48:04 2017