The asctime() function is defined in the time.h header file. This function returns the system-defined local time. Actually, asctime() function returns the pointer to the string which represents the time and day of the structure struct 'timeptr' argument.
char *asctime(const struct tm *timeptr); #where second should be a pointer
The asctime() function takes a single parameter 'timeptr' which is a pointer to the structure which contains a calendar time. The components of calendar time are,
struct tm {
int tm_sec; /* seconds, range 0 to 59 */
int tm_min; /* minutes, range 0 to 59 */
int tm_hour; /* hours, range 0 to 23 */
int tm_mday; /* day of the month, range 1 to 31 */
int tm_mon; /* month, range 0 to 11 */
int tm_year; /* The number of years since 1900 */
int tm_wday; /* day of the week, range 0 to 6 */
int tm_yday; /* day in the year, range 0 to 365 */
int tm_isdst; /* daylight saving time */
};
Parameter | Description | Required / Optional |
---|---|---|
timeptr | pointer to the tm object to be converted | Required |
The function returns date and time information as a string in the format Www Mmm dd hh:mm:ss yyyy.
Input | Return Value |
---|---|
if parameter | calendar time |
#include <stdio.h>
#include <time.h>
int main()
{
struct tm* p;
time_t ltm;
ltm = time(NULL);
p = localtime(<);
// using the asctime() function
printf("%s", asctime(p));
return 0;
}
Output:
Wed Aug 14 04:21:25 2019
#include <stdio.h>
#include <time.h>
int main (){
struct tm t;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6; t.tm_m
t.tm_mday = 25;
t.tm_year = 89;
t.tm_wday = 6;
puts(asctime(&t));
return(0);
}
Output:
Sat Mar 25 06:10:10 1989