Calculate Difference Between Two Time Periods in C


Write a C Program to calculate difference between two time periods.

Click Here to View the Solution!
#include <stdio.h>     
struct TIME {
    int seconds;
    int minutes;
    int hours;
    void differenceBetweenTimePeriod(struct TIME t1,
    struct TIME t2,
    struct TIME *diff);

int main() {
    struct TIME startTime, stopTime, diff;
    printf("Enter the start time. \n");
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
    printf("Enter the stop time. \n");
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &stopTime.hours,&stopTime.minutes, &stopTime.seconds);

    // Difference between start and stop time
    differenceBetweenTimePeriod(startTime, stopTime, &diff);
    printf("\nTime Difference: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
    printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
    printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
    return 0;

    // Computes difference between time periods
    void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff) {
    while (stop.seconds > start.seconds) {
        start.seconds += 60;

    diff->seconds = start.seconds - stop.seconds;
    while (stop.minutes > start.minutes) {
        start.minutes += 60;

    diff->minutes = start.minutes - stop.minutes;
    diff->hours = start.hours - stop.hours;
Click Here to View the Output!
Enter the start time. 
Enter hours, minutes and seconds: 6 40 10
Enter the stop time. 
Enter hours, minutes and seconds: 2 30 05
Time Difference: 6:40:10 - 2:30:5 = 4:10:5

Click Here to View the Explanation!
  • This program is used to calculate the difference between two time periods by using Structures.
  • A Structure TIME is created with three integer members seconds, minutes and hours.
  • A void type function differenceBetweenTimePeriod is created taking struct TIME type variables t1, t2 and a pointer variable diff.
  • In main(), The program requests the user to enter the two time periods by asking the user for the hours, minutes and seconds for both the start time and the stop time and stores them in the corresponding variables by referencing through Struct.
  • The main method then prints the start time, the stop time and finally the difference of the time between the two through the call by reference technique.
  • The differenceBetweenTimePeriod function is initialized which first checks whether the seconds of the stop time are greater than that of the start time, if so, the minutes of the start time are decremented and 60 is added to the start time seconds until the seconds of the start time become greater then that of the stop time.
  • The difference of the seconds is then calculated by subtracting the stop time seconds from the start time seconds and stored in diff.
  • Similarly, the minutes of the stop time are checked that whether they are greater than the minutes of the start time.
  • Finally, the difference of the minutes and seconds is calculated by subtracting the minutes and hours of the stop time from that of the start time.

%d bloggers like this: