The asctime() function is defined in time.h header file. This function returns the pointer to the string that contains the information stored in the structure pointed to struct tm type. This function is used to return the local time defined by the system.
char *asctime(const struct tm* tm_ptr);
0Parameters: This function accepts single parameter time_ptr i.e pointer to the tm object to be converted.
Return Type: This function returns the calendar time in the form “Www Mmm dd hh:mm:ss yyyy”, where:
- Www: represents the day in three letter abbrivated (Mon, Tue, Wed.., )
- Mmm: represents the month in three letter abbrivated (Jan, Feb, Mar.., )
- dd: represents the date in two digits (01, 02, 10, 21, 31.., )
- hh: represents the hour (11, 12, 13, 22…, )
- mm: represents the minutes (10, 11, 12, 45…, )
- ss: represents the seconds (10, 20, 30…, )
- yyyy: represents the year in four digits (2000, 2001, 2019, 2020…, )
Below program demonstrate the asctime() function in C:
Wed Aug 14 04:21:25 2019
This function is used to convert the given calendar time into a textual representation. We can’t modify the output calendar time in asctime() function whereas we can modify the calendar time in asctime_s() function. The general syntax of asctime_s is “Www Mmm dd hh:mm:ss yyyy“.
errno_t asctime_s(char *buf, rsize_t bufsz, const struct tm *time_ptr)
Parameters: This function accepts three parameters:
- time_ptr: pointer to a tm object specifying the time to print
- buf: pointer to a user-supplied buffer at least 26 bytes in length
- bufsz: size of the user-supplied buffer
Return Value: This function returns pointer to a static null-terminated character string holding the textual representation of date and time. The original calendar time will be obtained from the asctime() function.
Note: In some C-compilers asctime_s() won’t be supported. We can use strftime() function instead of asctime_s() function.
Below program investigates the asctime_s() function in C:
Wed Aug 14 04:33:54 2019
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- isalpha() and isdigit() functions in C with cstring examples.
- Commonly used String functions in C/C++ with Examples
- Write one line functions for strcat() and strcmp()
- Functions that are executed before and after main() in C
- fill() and fill_n() functions in C++ STL
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- strdup() and strndup() functions in C/C++
- atol(), atoll() and atof() functions in C/C++
- Static functions in C
- Can static functions be virtual in C++?
- Functions that cannot be overloaded in C++
- Pure Functions
- Can virtual functions be inlined?
- Macros vs Functions
- Return from void functions in C++
- Forward List in C++ | Set 2 (Manipulating Functions)
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- exec family of functions in C
- Functions in C/C++
- C | Functions | Question 11
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.