Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to find number of days b/w two given dates

Posted By: Easy Tutor     Category: C++ Programming     Views: 7110

A C++ program to find number of days b/w two given dates.

Code for Program to find number of days b/w two given dates in C++ Programming

 #include<iostream.h>
 #include<conio.h>


 /*************************************************************************///------------------------------  day  ----------------------------------///*************************************************************************/class day
    {
       private:
        int date;
        int month;
        int year;

       public:
        day()  {  date=0; month=0; year=0;  }
        void get_date();
        void show_days(day,day);
        void show_date();
        int next_month(int,int);
        int next_year(int);
        intoperator>(day);
    };

 /*************************************************************************///---------------------------  get_date( )  -----------------------------///*************************************************************************/void day::get_date()
    {
       cout<<"\tEnter the date = ";
       cin>>date;

       cout<<"\tEnter the month = ";
       cin>>month;

       cout<<"\tEnter the year = ";
       cin>>year;
    }

 /*************************************************************************///--------------------------  show_date( )  -----------------------------///*************************************************************************/void day::show_date()
    {
       cout<<"\n\t Date is     ";
       cout<<((date<10)?"0":"")<<this->date<<" : ";
       cout<<((month<10)?"0":"")<<this->month<<" : "<<this->year<<endl;
    }

 /*************************************************************************///---------------------------  show_days(day,day)  ----------------------///*************************************************************************/void day::show_days(day d1,day d2)
    {
    long d=0;

    if(d2.date<d1.date && d2.month!=d1.month && d2.year>=d1.year)
       {
          d2.date+=next_month(d2.month-1,d2.year);
          d2.month--;
       }

    for(int count_1=d1.date;count_1<d2.date;count_1++)
       d++;

    if(d2.month<d1.month && d2.year>d1.year)
       {
          d2.month+=12;
          d2.year--;
       }

    for(int count_2=d1.month;count_2<d2.month;count_2++)
       d+=next_month(count_2,d1.year);

    for(int count_3=d1.year;count_3<d2.year;count_3++)
       d+=365+next_year(count_3);

    cout<<"\t Number of days b/w date1 & date2 = "<<d<<" days"<<endl;
    }

 /*************************************************************************///---------------------------  operator>(day)  --------------------------///*************************************************************************/int day::operator>(day d)
    {
       long d1;
       long d2;

       d2=date+(month*30)+(year*360);
       d1=d.date+(d.month*30)+(d.year*360);
       return (d2>d1?1:0);
    }

 /*************************************************************************///-------------------------  next_month(int,int)  -----------------------///*************************************************************************/int day::next_month(int m,int y)
    {
       int d;

       if(m<1)
      m+=12;

       elseif(m>12)
      m-=12;

       switch(m)
      {
         case 1 : d=31;
              break;

         case 2 : d=28+next_year(y);
              break;

         case 3 : d=31;
              break;

         case 4 : d=30;
              break;

         case 5 : d=31;
              break;

         case 6 : d=30;
              break;

         case 7 : d=31;
              break;

         case 8 : d=31;
              break;

         case 9 : d=30;
              break;

         case 10 : d=31;
              break;

         case 11 : d=30;
              break;

         case 12 : d=31;
              break;
      }

       return d;
    }

 /*************************************************************************///---------------------------  next_year(int)  --------------------------///*************************************************************************/int day::next_year(int y)
    {
       if(y%4==0 || y%4==0 || y%100==0)
      return 1;

       elsereturn 0;
    }

 /*************************************************************************//*************************************************************************///-----------------------------  Main( )  -------------------------------///*************************************************************************//*************************************************************************/

 main()
    {
       clrscr();

       day date_1;
       day date_2;
       day date_3;

       cout<<"\n Enter the date in the pattern i.e  dd/mm/yy "<<endl;

       cout<<"\n  First Date :"<<endl;
       date_1.get_date();

       cout<<"\n  Second Date :"<<endl;
       date_2.get_date();

       cout<<"\n********** First Date **********"<<endl;
       date_1.show_date();

       cout<<"\n********** Second Date **********"<<endl;
       date_2.show_date();

       cout<<"\n********** Result **********\n"<<endl;
       if(date_1>date_2)
      date_3.show_days(date_2,date_1);

       else
      date_3.show_days(date_1,date_2);

       getch();
       return 0;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to find number of days b/w two given dates Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to find number of days b/w two given dates is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!