Pages

Friday, October 12, 2018

Time difference using System Date and Time


Following C program will detect the time difference between two time intervals using System Date and Time function.

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

int main()
{
time_t t1, t2;

time(&t1);
printf("Time reading 1: \nSystem date and time is: %s\n",ctime(&t1));

printf("Wait for a moment...\nPress any key to continue....\n\n");
getch();

//Program logic.....

time(&t2);
printf("Time reading 2:\nSystem date and time is: %s\n\n",ctime(&t2));
   
printf("The time difference = %d sec. ", t2-t1);

return 0;
}


Output of Program

Time reading 1:
System date and time is: Fri Oct 12 10:39:45 2018

Wait for a moment...
Press any key to continue....

Time reading 2:
System date and time is: Fri Oct 12 10:39:48 2018

The time difference = 3 sec.

No comments:

Post a Comment