C++ Program to Calculate the Difference in Time

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

This article will discuss various methods to calculate the difference between two time periods. But before knowing about these methods of calculating the difference between two time periods, we must know what time period is. A time period is uniquely defined as triplets of hours, minutes, and seconds. To calculate the difference in time, we require two time periods.

Introduction

In this article, we will calculate the difference between two given time periods in C++. To calculate the difference between time periods, we require two time periods which are provided by the user. The time periods should be in the form of hours, minutes, and seconds. After taking the time periods, the difference between the two given time periods is calculated.

For example-:

Sharpen Your Fundamentals with Free Learning

Working

We used two methods to calculate the difference between the two time periods.

  • The first method uses the difftime() function, defined in the <ctime> header file. In this method, the difftime() function takes two parameters which are start_time and end_time. In order to set start_time and end_time, the time() function is used, which provides the current calendar time of type time_t.
  • The second method uses the structure in which we will take the hour, minute, and second values of both time periods and keep them in two variables defined by a structure. Then, we will find the difference between the two times and then print the values.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Program to Calculate the Difference in Time using difftime() Function

The difftime() function is defined in the <ctime> header file. The difftime() function is used to compute the difference between two times in seconds.

The syntax of the difftime() is:

Here in the above syntax, The difftime() takes two parameters. The first parameter is end, which represents the ending time, and the second parameter is start, which represents the beginning time.

Example:

Output:

Explanation: In the above C++ program, we used the difftime() function to calculate the difference in time. Here, the difference in time is the time taken by the user to answer the question. The difftime() takes two parameters, start, and end, and returns the difference between start and end times in seconds. Here, the time() function is used to get the current calendar time of type time_t.

Program to Calculate the Difference in Time Using Structure

A structure is a way to group various related variables into one place. Each variable in the structure is referred to as a member variable. The structure is almost similar to a class in that both contain a collection of data of different data types. But the main difference between class and structure is that structure members are public by default, and class members are private by default.

The syntax of the structure is:

Here in the above syntax, the structure is defined with the struct keyword. The data_type variable is the C++ variable of different data types like int, char, float, etc., and the StructureName is the name we want to give to our structure. Here is a simple example for a better understanding of the structures.

Output:

Example:

Output:

Explanation:

In the above C++ program, the user is asked to enter two time periods stored in structure variables time1 and time2, respectively. We have defined three Time variables, time1, time2, and time_diff, to hold the first time, the second time, and the difference between the first and second time. Then, the CalulateTimeDifference() function is called to calculate the difference between time1 and time2. In CalulateTimeDifference() function, we are checking if the secs value of time2 is greater than the secs value time1. If yes, we are incrementing the secs value of time1 by 60 and decrementing the mins value of time1 by 1. Similarly, we are checking for the mins values and incrementing the mins value of time1 by 60 and decrementing the hours value time1 by 1. In the end, return the time_diff variable, which contains the difference between time1 and time2.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • A time period is uniquely defined as triplets of hours, minutes, and seconds.
  • The difftime() function can be used to calculate the difference between two times in seconds.
  • The difftime() function is defined in the <ctime> header file.
  • The difftime() function takes two parameters. The first parameter is end, which represents the ending time, and the second parameter is start, which represents the beginning time.
  • We can use structure to calculate the difference between two time periods. In which we use three member variables to store hours, minutes, and seconds.
  • A structure is a collection of variables of various data types that go under a single name.