C Standard Library

C <time.h> - ctime() Function



The C <time.h> 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 <stdio.h>
#include <time.h>
 
int main (){
  time_t t = time(NULL);

  //displaying the result 
  printf("Current local time & date: %s", ctime(&t));   
  
  return 0;
}

The output of the above code will be:

Current local time & date: Fri Apr 16 11:35:41 2021

❮ C <time.h> Library