OverIQ.com

C Program to print the earlier of the two dates

Last updated on July 27, 2020


The following C program asks the user to enter two dates and prints the earlier of the two dates.

  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
148
149
/**********************************************
 Program to print the earlier of the two dates 
 * 
 * Enter first date (MM/DD/YYYY): 20/10/2020
 * Enter second date (MM/DD/YYYY): 02/29/2001
 * 
 * 02/29/2001 comes earlier than 20/10/2020
 **********************************************/

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

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

    int is_leap = 0, is_valid = 1;

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

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

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

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

    if(year1 > year2)
    {
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
    }

    else if (year1 < year2)
    {
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
    }

    // year1 ==  year2
    else
    {
        if (mon1 ==  mon2)
        {
            if (day1 ==  day2)
            {
                printf("Both dates are the same.");
            }

            else if(day1 > day2)
            {
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
            }

            else
            {
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
            }
        }

        else if (mon1 > mon2)
        {
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
        }

        else 
        {
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
        }        
    }

    return 0; // return 0 to operating system
}

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

int check_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;

}

Expected Output: 1st run:

1
2
3
Enter the first date (MM/DD/YYYY): 05/10/2016
Enter the second date (MM/DD/YYYY): 04/09/2016
04/09/2016 comes earlier than 05/10/2016

2nd run:

1
2
3
Enter the first date (MM/DD/YYYY): 02/01/2008 
Enter the second date (MM/DD/YYYY): 02/01/2008
Both dates are the same.

How it works #

The program starts off by asking the user to enter two dates.

In line 27 and 32, we check whether the dates entered is valid or not using a user-defined check_date() function. To learn more about how check_date() function works visit this link.

If either of the dates is invalid, the program terminates with the appropriate error message.

In lines 39-85, we have an if-else statement which compares the two dates. The algorithm to compare the dates work as follows:

  • If year1 > year2, the second date comes earlier than the first.
  • If year1 < year2, the first date comes earlier than the second.
  • If year1 == year2. The following conditions are possible:
    • If mon1 == mon2, The following conditions are possible:
      • If day1 == day2, both dates are the same.
      • If day1 > day2, then the second date comes earlier than the first.
      • If day1 < day2, then the first date comes earlier than the second.
    • If mon1 > mon2, the second date comes earlier than the first.
    • Otherwise, mon1 < mon2 and the first date comes earlier than the second.