
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Calculate Difference Between Two Time Period
There are two time periods provided in the form of hours, minutes and seconds. Then their difference is calculated. For example −
Time period 1 = 8:6:2 Time period 2 = 3:9:3 Time Difference is 4:56:59
A program that calculates the difference between two time periods is given as follows −
Example
#include <iostream> using namespace std; int main() { int hour1, minute1, second1; int hour2, minute2, second2; int diff_hour, diff_minute, diff_second; cout << "Enter time period 1" << endl; cout << "Enter hours, minutes and seconds respectively: "<< endl; cin >> hour1 >> minute1 >> second1; cout << "Enter time period 2" << endl; cout << "Enter hours, minutes and seconds respectively: "<< endl; cin >> hour2 >> minute2 >> second2; if(second2 > second1) { minute1--; second1 += 60; } diff_second = second1 - second2; if(minute2 > minute1) { hour1--; minute1 += 60; } diff_minute = minute1 - minute2; diff_hour = hour1 - hour2; cout <<"Time Difference is "<< diff_hour <<":"<< diff_minute <<":"<<diff_second; return 0; }
Output
The output of the above program is as follows −
Enter time period 1 Enter hours, minutes and seconds respectively: 7 6 2 Enter time period 2 Enter hours, minutes and seconds respectively: 5 4 3 Time Difference is 2:1:59
In the above program, two time periods are accepted from the user in the form of hours, minutes and seconds. This is given below −
cout << "Enter time period 1" << endl; cout << "Enter hours, minutes and seconds respectively: "<< endl; cin >> hour1 >> minute1 >> second1; cout << "Enter time period 2" << endl; cout << "Enter hours, minutes and seconds respectively: "<< endl; cin >> hour2 >> minute2 >> second2;
Then the difference between these two time periods is calculated using the method provided in the following code snippet −
if(second2 > second1) { minute1--; second1 += 60; } diff_second = second1 - second2; if(minute2 > minute1) { hour1--; minute1 += 60; } diff_minute = minute1 - minute2; diff_hour = hour1 - hour2;
Finally the time difference is displayed. This is given below −
cout <<"Time Difference is "<< diff_hour <<":"<< diff_minute <<":"<<diff_second;
- Related Articles
- Golang Program to Calculate Difference Between Two Time Periods
- C Program to calculate the difference between two time periods
- How to calculate time difference between two times or dates?
- Golang program to calculate difference between two slices
- Java Program to Calculate the difference between two sets
- Golang program to calculate the symmetric difference between two slices
- How can we calculate the difference between two time values in MySQL?
- Java Program to get the difference between two time zones by seconds
- How to calculate the difference between time in different MySQL columns?
- C program to calculate distance between two points
- Swift Program to Calculate Distance Between Two Points
- Python program to find difference between current time and given time
- How to calculate the difference between two dates in JavaScript?
- Difference Between Period and Spotting
- How to get time difference between two timestamps in seconds?

Advertisements