C Time Functions - C asctime






Converts time to a string.

Prototype

char* asctime (const struct tm * timeptr);

Parameter

This function has the following parameter.

timeptr
Pointer to a tm structure containing a calendar time.

Return

C-string containing the date and time in a human-readable format.

Example


#include <time.h>
#include <stdio.h>
 
int main(void)
{
    struct tm tm = *localtime(&(time_t){time(NULL)});
    printf("%s", asctime(&tm));
 
}

The code above generates the following result.





Example 2


#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, time, localtime, asctime */
/* w w w. j av  a2 s.c o  m*/
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  return 0;
} 

       

The code above generates the following result.