Time Difference Program

My goal is to calculate the difference between time 1 and time 2. Time 1 for example would be 11 00 am and time 2 would be 12 00 pm. The difference should be in minutes, thus would equal to 720 minutes. When executed my total time would be -60 minutes. That's because I have time1 - time2. How would I go on to solve this problem? Also how could I properly format the time as in 1:00? One more thing, how would I properly set the flags for AM and PM times?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include "stdafx.h"
#include<iostream>

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

int timeDifference(int hoursTime1, int minutesTime1, int total_mins1, int total_mins2,
	int hoursTime2, int minutesTime2, bool isPM, bool isAM);

int main()
{
	int hoursTime1, hoursTime2;
	int minutesTime1, minutesTime2;
	char time1, time2;
	bool isPM = 0, isAM = 0;

	cout << "Please enter the time 1: (Example (hours minutes) ie. (4 45): ";
	cin >> hoursTime1 >> minutesTime1;

	cout << "Please indicate Time 1 whether it is AM or PM (enter A for AM; enter P for PM: ";
	cin >> time1;

	if (( time1 == 'p') || (time1 == 'P'))
	{
		isPM = 1;
	}

	else

		cin >> time1;
	if (( time1 == 'a') || (time1 == 'A'))
	{
		isAM = 1;
	}

	int total_mins1 = (hoursTime1 * 60) + (minutesTime1);
	cout << "Total minutes of Time 1 is: " << total_mins1 << "\n";


	cout << "Please enter the time 2: (Example 12 30) ";
	cin >> hoursTime2 >> minutesTime2;

	cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM: ";

	cin >> time2;
	if (( time2 == 'p') || (time2 == 'P'))
	{
		isPM = 1;
	}

	else

		cin >> time2;
	if (( time2 == 'a') || (time2 == 'A'))
	{
		isAM = 1;
	}

	int total_mins2 = (hoursTime2 * 60) + (minutesTime2);
	cout << "Total minutes of Time 2 is: " << total_mins2 << "\n";


	cout << "The time difference is: ";
	cout << timeDifference(hoursTime1, minutesTime1, total_mins1, total_mins2, hoursTime2, minutesTime2, isPM, isAM);

	return 0;
}

int timeDifference(int hoursTime1, int minutesTime1, int total_mins1, int total_mins2,
	int hoursTime2, int minutesTime2, bool isPM, bool isAM)
{

	int difference;

	difference = total_mins1 - total_mins2;

	if(isPM)
	{
		if((hoursTime1 >= 1) && (hoursTime1 < 12)) //if hours time 1 is >= 1 and hours time 1 < 12 then hoursTime1 is 12
		{
			hoursTime1 += 12;
		}
	}
	if(isAM)
	{
		(hoursTime2 == 12);
		{
			hoursTime1 += 12;
		}
	}
	return difference;
}


Last edited on
closed account (jw6XoG1T)
well im still not exactly sure what you're trying to do here, but first off i would tell you that to get the difference of minutes between the two times, you would want to do this:

total_minutes_of_time_two - total_minutes_of_time_one

this would give you the amount of minutes between each time. Next, to get the hour in PM formatted right, you would do something along the lines of

1
2
3
4
5
6

if (someHour > 12)
{
     someHour -= 12;
     isPM = true;
}


So say if "someHour" was 17, then 17 minus 12 would give you an answer of 5PM. This also brings to mind another thing. Instead of having both "isPM" and "isAM", just stick to having one bool value. In my example i just chose to keep "isPM". This way if something is still in the "AM", then set "isPM" to false and vise versa.

Now to get the hours out of the total difference of minutes you would do:

totalHours = totalMinutes % 60.

Not sure what else you need help with but ill try and help you out if you want more ;)
Alright, so I'm using a new time to see if my program works the other way around. I am specifying 11:00 am for time 1 and 1:00 pm for time 2. I should get an answer of 120 minutes. Instead I get an output of 600 minutes which is equivalent to 9pm.

This is what I'm using as my timeDifference function. Are my flags for pm and am setup correctly throughout my program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
difference = total_mins2 - total_mins1;
	
		if(hoursTime1 > 12) //if hours time 1 is >= 1 and hours time 1 < 12 then hoursTime1 is 12
		{
			hoursTime1 -+ 12;
			isPM = true;
		}
		else
		if(total_mins1 > total_mins2)
		{
			difference = total_mins1 - total_mins2;
		}
	
	return difference;

Last edited on
closed account (jw6XoG1T)
you put in the first if statement "hoursTime1 -+ 12;"...it should be -=
Topic archived. No new replies allowed.