Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to calculate the difference between two time periods in C++

2 Answers

0 votes
#include <iostream>

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

void TimeDifference(struct TIME startTM, struct TIME endTM, struct TIME *diff);

int main()
{
	struct TIME startTime, endTime, diff;

	startTime.hours = 11;
	startTime.minutes = 21;
	startTime.seconds = 17;

	endTime.hours = 8;
	endTime.minutes = 05;
	endTime.seconds = 35;

	TimeDifference(startTime, endTime, &diff);

	std::cout << diff.hours << ":" << diff.minutes << ":" << diff.seconds << std::endl;

	return 0;
}

void TimeDifference(struct TIME startTM, struct TIME endTM, struct TIME *diff)
{
	if (endTM.seconds > startTM.seconds)
	{
		startTM.minutes--;
		startTM.seconds += 60;
	}

	if (endTM.minutes > startTM.minutes)
	{
		startTM.hours--;
		startTM.minutes += 60;
	}

	diff->seconds = startTM.seconds - endTM.seconds;
	diff->minutes = startTM.minutes - endTM.minutes;
	diff->hours = startTM.hours - endTM.hours;
}

/*
run:

3:15:42

*/

 



answered Jun 15, 2017 by avibootz
edited Jan 27, 2018 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

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

void TimeDifference(struct TIME t1, struct TIME t2, struct TIME *diff);

int main()
{
	struct TIME t1, t2, diff;

	cout << "Write start time: hours, minutes, seconds: ";
	cin >> t1.hours >> t1.minutes >> t1.seconds;

	cout << "Write stop time: hours, minutes, seconds: ";
	cin >> t2.hours >> t2.minutes >> t2.seconds;

	TimeDifference(t1, t2, &diff);

	cout << endl << "difference = " << diff.hours << ":" 
		                            << diff.minutes << ":" 
		                            << diff.seconds << endl;

	return 0;
}
void TimeDifference(struct TIME t1, struct TIME t2, struct TIME *diff)
{

	if (t2.seconds > t1.seconds)
	{
		t1.minutes--;
		t1.seconds += 60;
	}

	diff->seconds = t1.seconds - t2.seconds;
	if (t2.minutes > t1.minutes)
	{
		t1.hours--;
		t1.minutes += 60;
	}
	diff->minutes = t1.minutes - t2.minutes;
	diff->hours = t1.hours - t2.hours;
}

/*
run:

Write start time: hours, minutes, seconds: 13 18 23
Write stop time: hours, minutes, seconds: 16 15 12

difference = -3:3:11

*/

 



answered Jan 27, 2018 by avibootz
edited Jan 27, 2018 by avibootz

Related questions

1 answer 157 views
1 answer 128 views
1 answer 121 views
1 answer 131 views
1 answer 134 views
...