Search This Blog

Showing posts with label current date. Show all posts
Showing posts with label current date. Show all posts

How to Get Time and Date in C - C Program to Read and Display Time and Date

In this post, we will discuss how to read system time (current time) and date in c. To get system time and date, we use the time.h header file. It contains functions related to time and date. The function time() reads the current time and stores in the variable given as parameter. For example, if we call time(now), then the variable now is assigned the current date and time value. This parameter should be of type time_t. A sample code is:

time_t now;
time ( &now );

Note that we should pass the reference to a variable of type time_t.  But, the value stored by the function time() contains all information including date and time. So, the value appears to be an unintelligible random number. There fore, to extract hours, minutes, seconds, year, month, day of month etc from this value, we should convert into an object of structure struct tm. The function localtime() converts the value for us. This conversion to date and time can be done as follows: