C Program To Display Current Date and Time
Learn How To Display Current Date and Time in C Programming Language. This C Code demonstrates the use of asctime() and strftime() methods in C Programming to Print System Date and Time.
There are numerous built-in Library methods to Print System Date and Time viz., time(), ctime(), strftime(), asctime(), gmtime(), localtime() and many more. All these methods are defined in the Time Library with its Header file as time.h.
The strftime() method is preferred by ANSI C. This method returns the Number of Bytes that enables better Error Control Management if the String is too long to Print. The asctime() method may generate the problem of Buffer Overflow which are managed well in the strftime() method.
The asctime() and asctime_r() methods have, therefore, become obsolete and is, therefore, rarely used by the C Programmers. These methods also do not have Timezone information. This C Program, therefore, fetches the Date and Time into the String and then the String is printed. The struct tm stores the Date and Time. time_t is used to store the Calendar Time in it.
Method 1: C Program To Display Current Date and Time using strftime() method
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <time.h> #include <stdio.h> int main() { char temp[100]; time_t current_time = time(NULL); struct tm *tm = localtime(¤t_time); strftime(temp, sizeof(temp), "%c", tm); printf("\nCurrent Date and Time:\n"); printf("\n%s\n\n", temp); return 0; } |
Method 2: C Code To Print System Date and Time using asctime() method
1 2 3 4 5 6 7 8 9 10 11 | #include <time.h> #include <stdio.h> int main() { time_t current_time = time(NULL); struct tm *tm = localtime(¤t_time); printf("\nCurrent Date and Time:\n"); printf("%s\n", asctime(tm)); return 0; } |
Output

If you have any Doubts or Compilation Errors in this C Program To Display Current Date and Time, let us know about it in the Comment Section below.
Can we make a C Program that automatically updates the date and time?