C asctime()

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

 

asctime() Parameters: 

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

asctime() Return Value

The function returns date and time information as a  string in the format Www Mmm dd hh:mm:ss yyyy.

  • Www: represents the day in three letter abbrivated (Mon, Tue.., )
  • Mmm: represents the month in three letter abbrivated (Jan, Feb.., )
  • dd: represents the date in two digits (01, 02, 10.., )
  • hh: represents the hour (11, 12, 13…, )
  • mm: represents the minutes (10, 11, 12…, )
  • ss: represents the seconds (10, 20, 30…, )
  • yyyy: represents the year in four digits (2000, 2001, 2019…, )
Input Return Value
if parameter calendar time

 

Examples of asctime() 

Example 1: Working of asctime() function in C?




#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

Example 2: How asctime() works in C?


#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