C program to get current System Date and Time in Linux.

This program will get the current system date and time in Linux operating system using GCC or G++ compiler.

System local time can be extracted by using struct tm, and function localtime() which are declared in <time.h> header file. Following are the members of tm structure: tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec.

Print system Date and Time in Linux using C program


/*C program to get current system date and time in Linux.*/

#include <stdio.h>
#include <time.h>

int main()
{
    time_t T= time(NULL);
    struct  tm tm = *localtime(&T);
    
    printf("System Date is: %02d/%02d/%04d\n",tm.tm_mday, tm.tm_mon+1, tm.tm_year+1900);
    printf("System Time is: %02d:%02d:%02d\n",tm.tm_hour, tm.tm_min, tm.tm_sec);

    return 0;
}

    System Date is: 13/03/2014
    System Time is: 12:40:59