Link to home
Start Free TrialLog in
Avatar of RAMESH_GUPTA
RAMESH_GUPTA

asked on

DIFFERENCE OF TWO DATES

Want to find out the Difference of two dates when entered in MM/DD/YYYY format.The result will be in the number of days.
Avatar of RAMESH_GUPTA
RAMESH_GUPTA

ASKER

Any body ho will give the Ans I will be pleased to see the Answer of that Question

#include<stdio.h>
struct date {
        int MM,DD,YYYY;
};

int days[13]={0,31 ,28,31,30,31,30,31,31,30,31,30,31};
int leap=0;
struct date sub(struct date d1,struct date d2) {
        struct date temp;
        temp.DD = d1.DD - d2.DD;
        temp.MM = d1.MM - d2.MM;
        temp.YYYY = d1.YYYY-d2.YYYY;
        if(temp.MM<0){
                temp.MM += 12;
                temp.YYYY -=1;
        }
        if(temp.DD<0){
                if(temp.MM<1) {
                        temp.MM +=12;
                        temp.YYYY -=1;
                }
                temp.MM -=1;
                temp.DD += days[d1.MM]+leap; /*temp.MM];*/
                /* 31 ,28,29,30 will depend on
                 * temp.MM , i.e. iif it is
                 * 1 then 31 , 2 the 28/29 etc.*/
        }
        return temp;
}

main(){
        struct date d1,d2,d3;
        printf("\nEnter date one MM DD YYYY format : ");
        scanf("%d%d%d",&d1.MM,&d1.DD,&d1.YYYY);
        leap = (d1.YYYY %4 ==0)?1:0;
        printf("\nEnter date two MM DD YYYY format : ");
        scanf("%d%d%d",&d2.MM,&d2.DD,&d2.YYYY);
        d3=sub(d1,d2);
        printf("\nsub: \ndays: %d months: %d years: %d ",d3.DD,d3.MM,d3.YYYY);
        exit(0);
}

/* please make some changes for leap yr if necessary */

Sorry, in above soln
comment at line # 23 is :
/* 31 28 30 .. etc will depend on d1.MM and leap
not on temp.MM */
/* thanks */
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing has happened on this question in more than 13 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by ozo (it's a duplicate question, but ozo's solution is too elegant to delete).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer