469
In this example we show you how to display the current date and time in C
We will include the time library and then use the function char *ctime(const time_t *timer) which returns a string representing the localtime.
The returned string has the following format: Www Mmm dd hh:mm:ss yyyy.
Www is the weekday
Mmm the month in 3 character letter format e.g Apr
dd is the day of the month
hh:mm:ss is the time in hours, minutes and seconds
yyyy is the year.
Example
#include<stdio.h> #include<time.h> int main() { time_t theTime; // not a primitive datatype time(&theTime); printf("\nThe date and time is : %s", ctime(&theTime)); return 0; }
When I ran this I saw the following
The date and time is : Mon Apr 18 17:41:55 2022