The asctime() function is defined in <ctime> header file.
asctime() prototype
char* asctime(const struct tm * time_ptr);
The asctime() function takes a pointer to tm
object as its parameter and returns a text representation for a given calendar time of the form:
Www Mmm dd hh:mm:ss yyyy
Type |
Description |
Obtained from |
Values |
---|---|---|---|
Www |
3 letter day of week |
|
Mon to Sun |
Mmm |
3 letter month name |
|
Jan to Dec |
dd |
2 digit day of month |
|
00 to 31 |
hh |
2 digit hour |
|
00 to 23 |
mm |
2 digit minute |
|
00 to 59 |
ss |
2 digit second |
|
00 to 59 |
yyyy |
4 digit year |
|
4 digit number |
asctime() Parameters
- time_ptr: pointer to a tm object to be converted.
asctime() Return value
- Pointer to a null terminated string the points to the character representation of the given time.
Example: How asctime() function works?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t curr_time;
time(&curr_time);
cout << "Current date and time: " << asctime(localtime(&curr_time));
return 0;
}
When you run the program, the output will be:
Current date and time: Tue Mar 21 13:52:57 2017