Find the amount of time between dates-times

My task for this program is to have a user prompt in two dates and times. The first being the starting date and time, the second for the ending date and time.
I want to store the given information into structs.

The condition on the date is between 1/1/100 and 12/31/2000.

I have 4 main functions:
- Prompt for Date
- Prompt for Time
- Validate and compute Date-Time between the 2 sets of information
- Display result to the screen

The main body is to pretty much call for these functions and almost nothing else besides maybe some declarations and setting up the structs.

The format of the input for date would be <year><month><day>, where year is a 4 digit number, and 2 digits for month and day. The format of the input for time would be hr:mn:sc.

Can the input for both of these be entered such that:
"Please input date in the order of year, month, day with spaces in between."
"' ' time in the order of hour, minute, second with space in between."

After, the function for computing the time in between will basically return the years and months in between dates, followed by the leftover days, and the time.

Speaking of which, time will be recorded in a 24 hour format.

As for considering leap years into the entire equation, there was a wikipedia page that stated the format in which to distinguish a leap year from a common year, so that'll probably be within the date prompt function(?).

Is it possible, with the design that I have below, to accomplish this?
I think I know what coding algorithms to utilize, just want to make sure my plan will work before diving in.

Code is below. I appreciate the help!
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
#include <iostream>

using namespace std;

// Create function for determining whether current year is a leap or common yea\
r.                                                                              
// Cite Wikipedia article for leap year algorithm.                              
void leap(int& year)
{
  // When the year is not divisible by 4, then it is a common year.             
  // Otherwise when year is not divisible by 100, then it is a leap year.       
  // Otherwise when year is not divisible by 400, then year is a common year.   
  // Otherwise, year is a leap year.                                            
}

// Create function for reading and validating time.                                                                                                                                                                                                                                                                
{
  // Prompt user to enter a valid time between 1/1/100 and 12/31/2000.                                                                                                                                                                                                        

  // Validate the user time is within the given paramaters.                                                                                                                                                                                                                   
}

// Create a function for reading and evaluating two date-times.                                                                                                                                                                                                               
void date_time(int& date, int& time)
{
  // Compute the time in between the two given dates.                                                                                                                                                                                                                         

  // Report the result in difference in months, days, hours, minutes and seconds.                                                                                                                                                                                             
}

// Create a function to display the date-time and the time in between.                                                                                                                                                                                                        
void result(int& date_time)
{
  // Gets the results returned from the date-time function.                                                                                                                                                                                                                   

  // Displays the results in the following format.                                                                                                                                                                                                                            
  // <year><month><day> hr:mn:sc.                                                                                                                                                                                                                                             
}

int main()
{
  // Declare variables for year, hour, minute, and second.                                                                                                                                                                                                                    

  // Declare arrays for month and day.                                                                                                                                                                                                                                        

  // Create a structure to record the date.                                                                                                                                                                                                                                   

  // Create a structure to record the time                                                                                                                                                                                                                                    

  // Display to the user that the following prompts are for the starting date-time.                                                                                                                                                                                           

  // Call for function that will prompt for the time.                                                                                                                                                                                                                         

  // Call for function that will prompt for the date.                                                                                                                                                                                                                         

  // Display the starting date and time as provided by the user.                                                                                                                                                                                                              

  // Display to the user that the following prompts are for the ending date-time                                                                                                                                                                                              

  // Call for function that will prompt for the time.                                                                                                                                                                                                                         

  // Call for function that will prompt for the date.                                                                                                                                                                                                                         

  // Display the ending date and time as provided by the user.                                                                                                                                                                                                                

  // Call for function that will evaluate two date-times.                                                                                                                                                                                                                     

  // Call for function to display the results from evaluation.                                                                                                                                                                                                                

  return 0;
}
closed account (48T7M4Gy)
It might be an idea to allow for a menu in main, and possibly elsewhere in a function or two.
You're off to a great start. I've always used the same technique at the start of a project: figure out the methods, write comments that describe what they do, iterate until everything looks good and then fill in the code.

But you're missing a key piece here: the data. You should define how you plan to store the date and time. One thing will become immediately apparent: leap() currently takes a year as the parameter, but everything else is dealing with a date or time. So you need a way to extract the year from a date.
kemort, we haven't learned about allowing menus yet, so I'm unsure how I would implement that into my code.

dhayden, good point, I'll give that a shot!
closed account (48T7M4Gy)
On second reading you've covered what I am calling menus in you items flagged display etc. :)
Creating a menu function in the main with a do while loop with switch statements would work.
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
int main()
{
int myChoice;

do{

cout << "-----------MENU-------------";
cout << "1. Option 2";
cout << "2. Option 2";
cout << "etc.";

cin >> myChoice;

switch (myChoice)
{

case 1: // when user chooses "1".
cout << "You have chosen Option 1";
//You can call other functions into this.
break;

case 2: 
cout << "You have chosen Option 2";
break;

default:
cout << "etc";
}

} while (myChoice!=3);
//or some other option number or other condition
return 0;





}


Last edited on
The following comments contain my current code.
I'm all ears, I'd appreciate it.

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
#include <iostream>
#include <iomanip>

using namespace std;

struct store_date
{
  int year;
  int month;
  int day;
};

store_date start_date;
store_date end_date;
store_date final_date;

struct store_time
{
  int hour;
  int minute;
  int second;
};

store_time start_time;
store_time end_time;
store_time final_time;

const string month_name[12] = {"January", "Febuary", "March", "April", "May", "June", "July",
                               "August", "September", "October", "November", "December"};

// Create function for determining whether current year is a leap or common year.                                                                       
// Cite Wikipedia article for leap year algorithm.                                                                                                      
void Leap(int& year)
{
  // When the year is not divisible by 4, then it is a common year.                                                                                     
  // Otherwise when year is not divisible by 100, then it is a leap year.                                                                               
  // Otherwise when year is not divisible by 400, then year is a common year.                                                                           
  // Otherwise, year is a leap year.                                                                                                                    
}
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
94
95
96
// Create function for reading and validating time.                                                                                                     
void Time (struct store_time& begin_end, struct store_date& start_end)
{
  // Prompt user to enter a valid time between 00:00:00 and 23:59:59.                                                                                   
  cout << "Please enter the start hour, minute, second between 00:00:00 and 23:59:59." << endl;
  cin >> start_time.hour >> start_time.minute >> start_time.second;

  if ( start_time.second >= 60 )
    {
      cout << "Invalid input for seconds." << endl;
      return;
    }
  else if ( start_time.minute >= 60 )
    {
      cout << "Invalid input for minutes." << endl;
      return;
    }
  else if ( start_time.hour >= 24 )
    {
      cout << "Invalid input for hour." << endl;
      return;
    }
  else
    cout << endl;

  cout << "Please enter the end hour, minute, second between 00:00:00 and 23:59:59." << endl;
  cin >> end_time.hour >> end_time.minute >> end_time.second;

  if ( end_time.second >= 60 )
    {
      cout << "Invalid input for seconds." << endl;

      return;
    }
  else if ( end_time.minute >= 60 )
    {
      cout << "Invalid input for minutes." << endl;

      return;
    }
  else if ( end_time.hour >= 24 )
    {
      cout << "Invalid input for hour." << endl;

      return;
    }
  else
    cout << endl;

  // Validate the user time is within parameters.                                                                                                       
  cout << "The start time is: " << setfill('0') << setw(2) << start_time.hour << ':';
  cout << setfill('0') << setw(2) << start_time.minute << ':';
  cout << setfill('0') << setw(2) << start_time.second << endl;;
  cout << "The end time is: " << setfill('0') << setw(2) << end_time.hour << ':';
  cout << setfill('0') << setw(2) << end_time.minute << ':';
  cout << setfill('0') << setw(2) << end_time.second << endl;
  cout << endl;
}

// Create function for reading and validating a date.                                                                                                   
void Date (struct store_date& start_end)
{

  // Prompt user to enter a valid start and end date between 1/1/100 and 12/31/2000.                                                                    
  cout << "Please enter the start year, month, and day between 100/1/1 and 2000/12/31" << endl;
  cin >> start_date.year >> start_date.month >> start_date.day;
  cout << endl;

  cout << "Please enter the end year, month, and day between 100/1/1 and 2000/12/31" << endl;
  cin >> end_date.year >> end_date.month >> end_date.day;
  cout << endl;

  // Validate the date to be within set parameters.                                                                                                     
  // When start/end year is under 100, over 2000, or start year is greater then end year, it is invlaid.                                                
  if ( start_date.year < 100 || start_date.year > 2000 || start_date.year > end_date.year )
    cout << "Invalid start year. Please try again." << endl;
  else if( end_date.year < 100 || end_date.year > 2000 )
    cout << "Invalid end year. Please try again" << endl;

  // Otherwise, validate month, and display given dates.                                                                                                
  else
    {
      // When month number is less than 1 or greater than 12, input is invalid.                                                                         
      if ( start_date.month < 1 || start_date.month > 12 )
        cout << "Invalid starting month. Please try again" << endl;
      else if ( end_date.month < 1 || end_date.month > 12 )
        cout << "Invalid ending month. Please try again." << endl;
      // Otherwise, display dates.                                                                                                                      
      else
        {
          cout << "The start date is: " << start_date.day << ' ' << month_name[start_date.month-1] << ", " << start_date.year << endl;
          cout << "The end date is: " << end_date.day << ' ' << month_name[end_date.month-1] << ", " << end_date.year << endl;
          cout << endl;
        }
    }
}
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
// Create a function for reading and evaluating two date-times.                                                                                         
void Date_Time(struct store_time& begin_end, struct store_date& start_end)
{

  // Compute the summation of the two given times.                                                                                                      
  final_time.second = start_time.second + end_time.second;
  final_time.minute = start_time.minute + end_time.minute;
  final_time.hour = start_time.hour + end_time.hour;

  // Compute the summation of the two given dates                                                                                                       
  final_date.day = start_date.day + end_date.day;
  final_date.month = start_date.month + end_date.month;
  final_date.year = start_date.year + end_date.year;

  // When final second is over 60, subract 60.                                                                                                          
  if ( final_time.second >= 60 )
    {
      final_time.second = final_time.second - 60;
      final_time.minute = final_time.minute + 1;
      if ( final_time.minute >= 60 )
        {
          final_time.minute = final_time.minute - 60;
          final_time.hour = final_time.hour + 1;
          if ( final_time.hour >= 24 )
            {
              final_time.hour = final_time.hour -24;
              final_date.day = final_date.day + 1;
            }
        }
    }

  // Otherwise, when final minute is over 60, subtract 60.                                                                                              
  else if ( final_time.minute >= 60 )
    {
      final_time.minute = final_time.minute - 60;
      final_time.hour = final_time.hour + 1;
      if ( final_time.hour >= 24 )
        {
          final_time.hour = final_time.hour -24;
          final_date.day = final_date.day + 1;
        }
    }

  // Otherwise, when final hour is over 24, subtract 24.                                                                                                
  else if ( final_time.hour >= 24 )
    {
      final_time.hour = final_time.hour -24;
      final_date.day = final_date.day + 1;
    }
  else
    cout << endl;

  while ( final_date.day > 31 )
    {
      final_date.day = final_date.day - 31;
      final_date.month = final_date.month + 1;

      // When the final month number is over 12, then subtract 12.                                                                                      
      while ( final_date.month > 12 )
        {
          final_date.month = final_date.month - 12;
          final_date.year = final_date.year + 1;
        }
    }
  while ( final_date.month > 12 )
    {
      final_date.month = final_date.month - 12;
      final_date.year = final_date.year + 1;
    }

  cout << endl;
}
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
// Function will display start, end, and final date and time results.                                                                                   
void Result(struct store_time& begin_end, struct store_date& start_end)
{
  // Starting date and time.                                                                                                                            
  cout << "The summation of the start and end time and date is as follows:";
  cout << endl;
  cout << endl;
  cout << setfill('0') << setw(2) << start_date.month << '/';
  cout << setfill('0') << setw(2) << start_date.day << '/';
  cout << setfill('0') << setw(4) << start_date.year << " (";
  cout << setfill('0') << setw(2) << start_time.hour << ':';
  cout << setfill('0') << setw(2) << start_time.minute << ':';
  cout << setfill('0') << setw(2) << start_time.second << ')';

  cout << " + ";

  // Ending date and time.                                                                                                                              
  cout << setfill('0') << setw(2) << end_date.month << '/';
  cout << setfill('0') << setw(2) << end_date.day << '/';
  cout << setfill('0') << setw(4) << end_date.year << " (";
  cout << setfill('0') << setw(2) << end_time.hour << ':';
  cout << setfill('0') << setw(2) << end_time.minute << ':';
  cout << setfill('0') << setw(2) << end_time.second << ')';

  cout << " = ";

  // Final date and time.                                                                                                                               
  cout << setfill('0') << setw(2) << final_date.month << '/';
  cout << setfill('0') << setw(2) << final_date.day << '/';
  cout << setfill('0') << setw(4) << final_date.year << " (";
  cout << setfill('0') << setw(2) << final_time.hour << ':';
  cout << setfill('0') << setw(2) << final_time.minute << ':';
  cout << setfill('0') << setw(2) << final_time.second << ')';
  cout << endl;
  cout << endl;
  cout << "Given the information, the resulting date is: ";
  cout << final_date.year << ' ';
  cout << month_name[final_date.month-1] << ' ';
  cout << setfill('0') << setw(2) << final_date.day << endl;
  cout << endl;
}

int main()
{
  // Call for function that will prompt for the date.                                                                                                   
  Date(start_date);

  // Call for function that will prompt for the time.                                                                                                   
  Time(start_time, start_date);

  // Perform computation of the start and end date and time.                                                                                            
  Date_Time(start_time, start_date);

  // Display the results of the Date and Time computation.                                                                                              
  Result(start_time, start_date);

  return 0;
}
Sorry if this is extremely extensive. This obviously does not accomplish the task 100% but it is close. All I'm missing from completing this is the inclusion of leap year conditions and the varying days within each month.

Again, any feedback is welcome, and I appreciate the time spent analyzing my code!
Also, upon running through the program, the following is the output:


Please enter the start year, month, and day between 100/1/1 and 2000/12/31
1999 12 31

Please enter the end year, month, and day between 100/1/1 and 2000/12/31
2000 12 30

The start date is: 31 December, 1999
The end date is: 30 December, 2000

Please enter the start hour, minute, second between 00:00:00 and 23:59:59.
23 59 59

Please enter the end hour, minute, second between 00:00:00 and 23:59:59.
23 59 59

The start time is: 23:59:59
The end time is: 23:59:59


The summation of the start and end time and date is as follows:

12/31/1999 (23:59:59) + 12/30/2000 (23:59:59) = 01/31/4001 (23:59:58)

Given the information, the resulting date is: 4001 January 31

@gideonb

I am slightly confused about what your program does. In your original post, you wrote
After, the function for computing the time in between will basically return the years and months in between dates, followed by the leftover days, and the time.
but you seem to be adding the start and ending dates, along with adding the hours, minutes and seconds, then displaying the results. Shouldn't you be displaying, as showing in your latest output above,
1
2
3
The difference of the start and end time and date is as follows:

12/31/1999 (23:59:59) to 12/30/2000 (23:59:59) = 364 days and no hours, no minutes and no seconds.
??
Last edited on
closed account (48T7M4Gy)
Code bloat?
Yeah, I was mistaken in understanding the objective of the program, and oddly I'm suppose to find the summation of the two given dates. Weird, but I did it as requested. But your solution looks like it would work for a difference in dates.

Kemort, yeah I know, but because this was the longest program I've written successfully to date, I just wanted to check to see if anyone had any opinions on the structure and design of my code.
closed account (48T7M4Gy)
Just to make sure, the 'code bloat' comment referred to the previous uncertainty about the sense of adding two dates. The request made to you to include that particular functionality is bizarre to say the least. Maybe the intent was to add say 16 hours to a current date, say for a future reminder scenario. Just adding Jan 5 to Oct 17 has no application in any sort of 'reality' I am aware of.

Other than that your extensive coding looks pretty good. You might want to consider:

An aspect that stands out is the obvious repetition where the same tests are repeated when they are being applied to different times. Functions reduce this dramatically where you have one function able to process many different times, start, finish or whatever. Also compound if statements often reduce the complexities of multiple nested if statements that go on forever.
eg ( if (hours > 24 && ... more tests ...){ ... )

Also, you can take great advantage of (integer) modular arithmetic and save a lot. eg If you have a time where hours > 24 then hours = hour % 24, days = hours /24, no while's needed.

eg hours = 26 -> 1 day, 2 hours.

I leave it to you for -ve hours.

:)
Last edited on
Topic archived. No new replies allowed.