C++ Standard Library C++ STL Library

C++ <ctime> - ctime() Function



The C++ <ctime> ctime() function converts the value pointed by timer into calendar local time and then to a textual representation, as if by calling asctime(localtime(time)). The returned string has the following format:

Www Mmm dd hh:mm:ss yyyy

  • Www - the day of the week (one of Mon, Tue, Wed, Thu, Fri, Sat, Sun).
  • Mmm - the month (one of Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
  • dd - the day of the month
  • hh - hours
  • mm - minutes
  • ss - seconds
  • yyyy - years

The string is followed by a new-line character ('\n') and terminated with a null-character.

Syntax

char* ctime (const time_t * timer);

Parameters

timer Specify pointer to a time_t object specifying the time to print.

Return Value

Returns a C-string containing textual representation of date and time. The string may be shared between asctime() and ctime(), and may be overwritten on each invocation of any of these functions.

Example:

The example below shows the usage of ctime() function.

#include <iostream>
#include <ctime>
using namespace std;
 
int main (){
  time_t t = time(NULL);

  //displaying the result 
  cout<<"Current local time & date: "<<ctime(&t);   
  
  return 0;
}

The output of the above code will be:

Current local time & date: Fri Apr 16 11:31:17 2021

❮ C++ <ctime> Library