Difference between two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aionpower
    New Member
    • Oct 2013
    • 1

    Difference between two dates

    How i can easly calculate the difference between two dates ?

    Example :

    Date 1 = 12 March(3)
    Date 2 = 24 November(11)

    Result = 258 days

    Can anyone give me an example how to do this in C++ ?
    Btw i don't want to use date functions or something like that.I want to do it with simple math formula.Thank you!
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    when i started to Google,
    i found the answer how to do this in 'C' within 5 minutes....

    C Language Example # 36 Find Date Difference : Date Difference Month wise. * Program will find date difference between two dates. * You just need to enter to date in dd mm yyyy format..


    now you can improve your homework skills to convert this 'C' to some 'C++' code

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The easiest thing is to convert each date to a number of days and then subtract the two converted results.

      Most date conversion routines have a minimum date where earlier dates are not allowed. I have used Jan 1, 1970 but any date earlier than any date you expect will work. This is your minimum date.

      First, subtract the years in the date to be converted from the years in the minimum date to get years. Multiply the years by 365 and add this to the result.

      Second divide the years by 4 to get the leap days and add this to the result.

      Third, if the year 2000 is between the minimum date years and the date to be converted years, then subtract 1 from the result because years divisible by 400 are not leap years.

      Fourth, if the year on the date to be converted is a leap year (divisible by 4 but not by 400) and the month is earlier than March, then subtract 1 from the result because the leap day in this date has not yet occurred.

      Fifth, convert the month in the date to be converted to days and add this amount to the result. Use 28 for February since the leap day calculation was done in converting the years.

      Sixth, add the days in the date to be converted to the result.

      Seventh, get a result for each date and subtract the results.

      You may find using a date conversion function is preferable because it is already written and debugged. But do post again and let me know how you fared on this.

      Comment

      • Sherin
        New Member
        • Jan 2020
        • 77

        #4
        Code:
        #include<stdio.h> 
        #include<stdlib.h> 
        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;
        
        }
        Output:

        1st run:
        Code:
        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:

        Code:
        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.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You solution doesn't quite answer the question which I think wants the different in days.

          Additionally your valid_date function passes 0 and negative values for days as valid.

          Comment

          Working...