OverIQ.com

C Program to calculate the difference of two dates in years, months and days

Last updated on September 24, 2020


The following is a C program to calculate the difference of two dates in years, months and days. Make sure that the start date is earlier than the end date.

  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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************
 Program to calculate the number of days, months and years
 between two dates dates
 * 
 * Enter first date (MM/DD/YYYY): 05/20/2004
 * Enter second date (MM/DD/YYYY): 06/05/2008
 * 
 * Difference: 4 years 00 months and 15 days.
 *******************************************************************/

#include<stdio.h> // include stdio.h library
#include<stdlib.h> // include stdlib.h library
int valid_date(int date, int mon, int year);

int main(void)
{
    int day1, mon1, year1,
        day2, mon2, year2;

    int day_diff, mon_diff, year_diff;         

    printf("Enter start date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon1, &day1, &year1);

    printf("Enter end date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon2, &day2, &year2);

    if(!valid_date(day1, mon1, year1))
    {
        printf("First date is invalid.\n");        
    }

    if(!valid_date(day2, mon2, year2))
    {
        printf("Second date is invalid.\n");
        exit(0);
    }       

    if(day2 < day1)
    {      
        // borrow days from february
        if (mon2 == 3)
        {
            //  check whether year is a leap year
            if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0)) 
            {
                day2 += 29;
            }

            else
            {
                day2 += 28;
            }                        
        }

        // borrow days from April or June or September or November
        else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12) 
        {
           day2 += 30; 
        }

        // borrow days from Jan or Mar or May or July or Aug or Oct or Dec
        else
        {
           day2 += 31;
        }

        mon2 = mon2 - 1;
    }

    if (mon2 < mon1)
    {
        mon2 += 12;
        year2 -= 1;
    }       

    day_diff = day2 - day1;
    mon_diff = mon2 - mon1;
    year_diff = year2 - year1;

    printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);

    return 0; // return 0 to operating system
}

// function to check whether a date is valid or not

int valid_date(int day, int mon, int year)    
{
    int is_valid = 1, is_leap = 0;

    if (year >= 1800 && year <= 9999) 
    {

        //  check whether year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
        {
            is_leap = 1;
        }

        // check whether mon is between 1 and 12
        if(mon >= 1 && mon <= 12)
        {
            // check for days in feb
            if (mon == 2)
            {
                if (is_leap && day == 29) 
                {
                    is_valid = 1;
                }
                else if(day > 28) 
                {
                    is_valid = 0;
                }
            }

            // check for days in April, June, September and November
            else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
            {
                if (day > 30)
                {
                    is_valid = 0;
                }
            }

            // check for days in rest of the months 
            // i.e Jan, Mar, May, July, Aug, Oct, Dec
            else if(day > 31)
            {            
                is_valid = 0;
            }        
        }

        else
        {
            is_valid = 0;
        }

    }
    else
    {
        is_valid = 0;
    }

    return is_valid;

}

Try it now

Expected Output:

1st run:

1
2
3
Enter start date (MM/DD/YYYY): 08/05/2001
Enter end date (MM/DD/YYYY): 08/20/2001
Difference: 0 years 00 months and 15 days.

2nd run:

1
2
3
Enter start date (MM/DD/YYYY): 10/11/2005
Enter end date (MM/DD/YYYY): 05/20/2016
Difference: 10 years 07 months and 09 days.

How it works #

The process of calculating the difference of two dates in terms of years, months and days is quite simple. All we need to do is subtract day, month and year of the start date from the day, month and year of the end date, respectively.

1
2
3
date_diff = day2 - day1
mon_diff = mon2 - mon1
year_diff = year2 - year1

Note that here we are assuming that the start date is smaller than the end date and the difference of days, months and years will be positive.

However, there is one problem.

What if the difference of days and months isn't positive?

For example, consider the following two dates:

1
2
08/25/2001 (25 Aug 2001)
05/05/2005 (05 May 2005)

In this case, the first date is smaller than the second, but the difference in days and months is not positive.

1
2
3
4
5
6
7
date_diff = day2 - day1
date_diff = 5 - 25
date_diff = -20

mon_diff = mon2 - mon1
mon_diff = 5 - 8
mon_diff = -3

To handle cases like this we do the following:

If day2 < day1, we borrow a month that is before mon2 and add days in that month to day2. For example, if mon2 == 09 i.e September, then we borrow month August month and add days in August to day2.

day2 = day2 + 30. (there are 30 days in August)

Also, since we are borrowing a month we have to subtract 1 from mon2.

mon2 = m2 - 1

Similarly, If mon2 < mon1, then we borrow one 1 year i.e 12 months and add that many months to mon2.

mon2 = mon + 12

Again, just as with months, we have to subtract 1 from the year.

year2 = year2 - 1