What is asctime() in C?

asctime is a function in C, defined in the header file time.h. It takes a tm object as input and returns a pointer to a string containing the year, month, days, hours, minutes, and seconds in the Www mmm dd hh:mm:ss yyyy format.

Function syntax

Parameters

asctime takes in a single, mandatory, parameter which is a pointer to an object of type tm. Following is the declaration of the tm structure:

tm_sec: Seconds passed in a minute seconds (0-59)
tm_min: Minutes passed in an hour (0-59)
tm_hour: Hours passed in a day (0-23)
tm_mday: Days passed in a month (1-31)
tm_mon: Months passed in a year (0-11)
tm_wday: Days passed in a week (0-6)
tm_yday: Days passed in a year (0-365)
tm_year: Years passed since 1900

Return value

The asctime function returns a pointer to a string containing the year, month, days, hours, minutes, and seconds values stored in the tm object received as input. The pointed string is in the Www mmm dd hh:mm:ss yyyy format.

Www: three character abbreviation of the weekday with a capital first letter (Sun)

Mmm: three character abbreviation of the month with a capital first letter(e.g., Dec)

dd: two digit day of the month (01-31)

hh: two digit hour (0-23)

mm: two digit minute (0-59)

ss: two digit second (0-59)

yyyy: four digit year (e.g. 2021)

Example

The following example uses the time function to generate a UTC value for the current time. This value is then used to populate a tm object using the gmtime function. Finally, we use the asctime function to generate a string for the current time format: Www mmm dd hh:mm:ss yyyy.

#include <stdio.h>
#include <time.h>
int main () {
// creating an object to put the current time in
time_t current_time;
// inserting th current UTC time in the object
time(&current_time);
// creating a tm object to hold the output of the gmt function
struct tm *curr_t_struct;
curr_t_struct = gmtime(&current_time );
//printing the date string using the asctime function
printf("This today's date and time: %2s\n", asctime(curr_t_struct));
return(0);
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved