Created
December 16, 2017 17:33
-
-
Save mrtejaslol/cd0ec1357e1f510871c0ddc836079c83 to your computer and use it in GitHub Desktop.
this program calculates the difference between two dates implemented in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
//initializations | |
int dd2,mm2,yyyy2; | |
int dd1,mm1,yyyy1; | |
int dd,mm,yyyy; | |
//inputs | |
cout<<"enter current date format dd mm yyyy"<<endl; | |
cin>>dd2>>mm2>>yyyy2; | |
cout<<"enter birth date format dd mm yyyy"<<endl; | |
cin>>dd1>>mm1>>yyyy1; | |
//verify | |
cout<<"current date is "<<dd2<<"/"<<mm2<<"/"<<yyyy2<<endl; | |
cout<<"birth date is "<<dd1<<"/"<<mm1<<"/"<<yyyy1<<endl; | |
//calc | |
yyyy=yyyy2-yyyy1; | |
mm=mm2-mm1; | |
dd=dd2-dd1; | |
//case if dd is negative i.e. current dd is less than birth date | |
if(dd<0) | |
{ | |
mm--; | |
dd+=30; | |
} | |
//case if mm is negative i.e. current mm is less than birth date | |
if (mm<0) | |
{ | |
yyyy--; | |
mm+=12; | |
} | |
//final output | |
cout<<"Difference between dates is "<<dd<<" days "<<mm<<" months and "<<yyyy<<" years"<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And how can we calculate actual Number of days when there is a leap year