Time Passed Between Dates

Does anyone have any feedback on how I could improve this program in making it faster or/and shorter? Its functionality is this basically; you enter two dates and it calculates the time that has passed between those dates. It is quite long, and I know that that "if-statement tree" is really ugly, but I didn't know how I would do this without 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
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
#include <iostream>
#include <cmath>
using namespace std;

int determiningLength(int number){
    int digits=0;
    if(number >= 1000000 && number <= 9999999)
        number+=10000000;
    while(number>=1){
        number/=10;
        digits++;
    }
    return digits;
}

int main(){
    int firstdate, seconddate, test1, test2, digits1=0, digits2=0;
    double years1, months1, days1, years2, months2, days2;
    cout << "Enter first date (DDMMYYYY): "; cin >> firstdate;
    cout << "Enter second date (DDMMYYYY): "; cin >> seconddate;
    test1=firstdate; test2=seconddate; digits1=determiningLength(firstdate); digits2=determiningLength(seconddate);
    if(digits1==8 && digits2==8){
        days1 = test1/1000000; days2 = test2/1000000; test1=firstdate; test2=seconddate;
        months1 = floor(fmod(test1,1000000)/10000); months2 = floor(fmod(test2,1000000)/10000);
        years1 = fmod(test1,1000000)-months1*10000; years2 = fmod(test2,1000000)-months2*10000;
        if(years1==years2){
            if(months1==months2){
                if(days1==days2){
                    cout << "No time has passed between those dates!";
                }
                else{
                    days2-=days1;
                    cout << days2 << " days have passed between those dates!";
                }
            }
            else{
                months2-=months1;
                if(days1==days2){
                    cout << months2 << " months have passed between those dates!";
                }
                else{
                    if(days1>days2){
                        months2--;
                    }
                    days2=31-fabs(days2=days2-days1);
                    cout << months2 << " months and " << days2 << " days have passed between those dates!";
                }
            }
        }
        else{
            years2-=years1;
            if(months1==months2){
                if(days1==days2){
                    cout << years2 << " years have passed between those dates!";
                }
                else{
                    days2-=days1;
                    cout << years2 << " years and " << days2 << " days have passed between those dates!";
                }
            }
            else{
                if(months1>months2){
                    years2--;
                }
                months2=12-(fabs(months2-months1));
                if(days1==days2){
                    cout << years2 << " years and " << months2 << " months have passed between those dates!";
                }
                else{
                    if(days1>days2)
                        months2--;
                    days2=fabs(days2-=days1);
                    cout << years2 << " years, " << months2 << " months and " << days2 << " days have passed between those dates!";
                }
            }
        }
    }
    else
        cout << "The dates you entered were invalid!";
}
Last edited on
For starters, it's not accurate.

1
2
3
Enter first date (DDMMYYYY): 31012013
Enter second date (DDMMYYYY): 01022013
0 months and 30 days have passed between those dates!


Most date programs calculate the seconds since 1.1.1970 and then calculate differences. I would recommend looking at some examples of those.
Last edited on
I've now fixed that error! :) Thanks for finding it for me. I will also look into those other date programs that you talked about. Does anyone else have any feedback?
Last edited on
Anyone?
Topic archived. No new replies allowed.