C++ Program to Calculate Difference Between Two Time Period

In this tutorial, we will write a C++ program to find the difference between two time periods. Before that, you may go through the following topic in C++.

We will look at two different programs:

  • Using if statement
  • Using Structure

C++ Program to Calculate Difference Between Two Time Periods

#include <iostream>
using namespace std;

int main()
{
  int hour1, minute1, second1;
  int hour2, minute2, second2;
  int diff_in_hour, diff_in_minute, diff_in_second;

  cout << "Enter the first time period:" << endl;
  cout << "Hours, Minutes and Seconds respectively: " << endl;
  cin >> hour1 >> minute1 >> second1;

  cout << "Enter the second time period:" << endl;
  cout << "Hours, Minutes and Seconds respectively: " << endl;
  cin >> hour2 >> minute2 >> second2;

  if (second2 > second1)
  {
    minute1--;
    second1 += 60;
  }

  diff_in_second = second1 - second2;

  if (minute2 > minute1)
  {
    hour1--;
    minute1 += 60;
  }

  diff_in_minute = minute1 - minute2;

  diff_in_hour = hour1 - hour2;

  cout << "Time Difference between above two time period is: " << endl;
  cout << diff_in_hour << "hr " << diff_in_minute << "min " << diff_in_second << "sec";

  return 0;
}

Output:

Hours, Minutes and Seconds respectively:
8
25
68
Enter the second time period:
Hours, Minutes and Seconds respectively:
7
20
25
Time Difference between above two time period is:
1hr 5min 43sec


C++ Program to find difference between two time period using structure

#include <iostream>
using namespace std;

struct Time
{
  int hours;
  int minutes;
  int seconds;
};

//Function to compute the difference in time periods
void differenceFunction(struct Time tp1, struct Time tp2, struct Time *difference)
{

  if (tp2.seconds > tp1.seconds)
  {
    --tp1.minutes;
    tp1.seconds += 60;
  }

  difference->seconds = tp1.seconds - tp2.seconds;
  if (tp2.minutes > tp1.minutes)
  {
    --tp1.hours;
    tp1.minutes += 60;
  }
  difference->minutes = tp1.minutes - tp2.minutes;
  difference->hours = tp1.hours - tp2.hours;
}

//main function
int main()
{
  struct Time tp1, tp2, difference;

  cout << "Enter the first time period:" << endl;
  cout << "Hours, Minutes and Seconds respectively: " << endl;
  cin >> tp1.hours >> tp1.minutes >> tp1.seconds;

  cout << "Enter the second time period:" << endl;
  cout << "Hours, Minutes and Seconds respectively: " << endl;
  cin >> tp2.hours >> tp2.minutes >> tp2.seconds;

  differenceFunction(tp1, tp2, &difference);

  cout << endl << "Time Difference: " << tp1.hours << ":" << tp1.minutes << ":" << tp1.seconds;
  cout << " - " << tp2.hours << ":" << tp2.minutes << ":" << tp2.seconds;
  cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds;
  return 0;
}

Output:

Enter the first time period:
Hours, Minutes and Seconds respectively:
10
20
50
Enter the second time period:
Hours, Minutes and Seconds respectively:
7
30
30

Time Difference: 10:20:50 - 7:30:30 = 2:50:20