Wednesday 26 October 2016

Difference of days between two dates in C++ program

This is how you can find the number of days between two dates using c++ program in visual studio.
Dates can be anything like 13-01-2016 to 21-05-2017...

#include <iostream>                        
using namespace std;                       

int main() {                               
  int d1,m1,y1,d2,m2,y2;                     
  cout << " enter day1" <<endl;
  cin>>d1;
  cout << " enter m1" <<endl;
  cin>>m1;
  cout << " enter y1" <<endl;
  cin>>y1;
  cout << " enter d2" <<endl;
  cin>>d2;
  cout << " enter m2" <<endl;
  cin>>m2;
  cout << " enter y2" <<endl;
  cin>>y2;
  //now find difference
  int days,months,years;
days=d2-d1;
months=m2-m1;
years=y2-y1;
//converting into days
  int mdays,ydays;
  mdays=months*30;
  ydays=years*365;
  //total
  int total;
total=days+mdays+ydays;

  cout <<" total days = "<< total <<endl;
  
system ("pause");
return 0;
}

No comments:

Post a Comment

Feel free to comment.Team NUST is here to listen you.