C++ ctime()

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() Representation

Type Description Values
Www 3 letter days of a week Mon to Sun
Mmm 3 letter month names Jan to Dec
dd 2 digit days 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 number

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
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community