C method of printing a date and time as a string

  • 2020-04-02 03:16:24
  • OfStack

Ctime () function:
The header file:


#include <time.h>

Definition function:


char *ctime(const time_t *timep);

Ctime () converts the information in the time_t structure referred to by the parameter timep to the time and date representation used in the real world, and returns the result as a string. This function has been converted from time zone to local time and the string format is "Wed Jun 30 21:49:08 1993\n".

Note: this string may be corrupted if the associated time and date function is called again.

Return value: returns a string representing the current local time and date.

sample


#include <time.h>
main(){
  time_t timep;
  time (&timep);
  printf("%s", ctime(&timep));
}

perform


Sat Oct 28 10 : 12 : 05 2000

Asctime () function:

The header file:


#include <time.h>

Definition function:


char *asctime(const struct tm * timeptr);

Asctime () converts the information in the tm structure referred to by the parameter timeptr into the real world time and date representation method, and returns the result as a string. This function has been converted from time zone to local time, the string format is: "Wed Jun 30 21:49:08 1993\n"

Return value: this string may be corrupted if the associated time and date function is called again. This function differs from ctime in that the parameters passed in are of a different structure.

Additional note: returns a string representing the current local time and date.

sample


#include <time.h>
main(){
  time_t timep;
  time (&timep);
  printf("%s", asctime(gmtime(&timep)));
}

perform


Sat Oct 28 02:10:06 2000


Related articles: