ctime() Function in C/C++
Last Updated :
26 Jun, 2023
The ctime() function is define in the time.h header file. The ctime() function returns the string representing the localtime based on the argument timer.
Syntax:
char *ctime(const time_t *timer)
Parameters: This function accepts single parameter time_ptr. It is used to set time_t object that contains a time value.
Return Value: This function returns a string that contains the date and time which is in human readable form.
Example:
Www Mmm dd hh:mm:ss yyyy
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The description of results are given below:
- Www: Day of week.
- Mmm: Month name.
- dd : Day of month.
- hh : Hour digit.
- mm : Minute digit.
- ss : Second digit.
- yyyy : Year digit.
Example:
C++
#include <iostream>
#include <ctime>
int main() {
time_t one_hour_from_now = time (0) + 3600;
char * dt = ctime (&one_hour_from_now);
std::cout << "One hour from now, the date and time will be: " << dt << std::endl;
return 0;
}
|
C
#include <stdio.h>
#include <time.h>
int main () {
time_t curtime;
time (&curtime);
printf ( "Current time = %s" , ctime (&curtime));
return (0);
}
|
Output:
One hour from now, the date and time will be: Thu Apr 13 09:01:46 2023
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...