Format
#include <time.h> char *asctime(const struct tm *time);
Language Level: ANSI, POSIX, XPG4
The asctime function converts time, stored as a structure pointed
to by time, to a character string. You can obtain the time
value from a call to gmtime or localtime; both return a pointer
to a tm structure defined in <time.h>.
The string result that asctime produces contains exactly 26 characters and has the format:
"%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
The following are examples of the string returned:
Sat Jul 16 02:03:55 1994\n\0 or Sat Jul 16 2:03:55 1994\n\0
The asctime function uses a 24-hour-clock format. The days are abbreviated to: Sun, Mon, Tue, Wed, Thu, Fri, and Sat. The months are abbreviated to: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. All fields have constant width. Dates with only one digit are preceded either with a zero or a blank space. The new-line character (\n) and the null character (\0) occupy the last two positions of the string.
Note:
The time and date functions begin at 00:00:00 Universal Coordinate Time, January 1, 1970, and do not have an upper limit.
Return Value
The asctime function returns a pointer to the resulting character
string. There is no error return value.
Note: asctime, ctime, and other time functions may use a common, statically allocated buffer to hold the return string. Each call to one of these functions may destroy the result of the previous call.
Example
This example polls the system clock and prints a message
giving the current time.
#include <time.h> #include <stdio.h>
int main(void)
{
struct tm *newtime;
time_t ltime;
/* Get the time in seconds */ time(<ime);
/* Convert it to the structure tm */ newtime = localtime(<ime);
/* Print the local time as a string */
printf("The current date and time are %s",
asctime(newtime));
return 0;
/*******************************************************
The output should be similar to:
The current date and time are Fri Jun 28 13:51 1994 *******************************************************/ }
![]()
ctime -- Convert Time to Character String
gmtime -- Convert Time
localtime -- Convert Time
mktime -- Convert Local Time
Convert to Formatted Time
time -- Determine Current Time
printf -- Print Formatted Characters
<time.h>