Για κοίταξε και αυτό
#include <iostream>
#include <ctime>
#include <cstring>
#include <string.h>
#include <sstream>
#define TRUE 1
#define FALSE 0
#include <fstream>
#include <cstring>
#include <sstream>
#include <stdexcept>
int StrToInt( const std::string& s )
{
int result;
std::istringstream ss( s );
ss >> result;
if (!ss) throw std::invalid_argument( "StrToInt" );
return result;
}
int months_of_year[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int is_leap_year(int year)
{
int result;
if ( (year%4) != 0 ) // or: if ( year%4 )
result = FALSE; // means: if year is not divisible by 4
else if ( (year%400) == 0 ) // or: if ( !(year%400) )
result = TRUE; // means: if year is divisible by 400
else if ( (year%100) == 0 ) // or: if ( !(year%100) )
result = FALSE; // means: if year is divisible by 100
else // (but not by 400, since that case
result = TRUE; // considered already)
return ( result );
}
int number_of_days_in_the_year(int year)
{
return(is_leap_year(year))? 366 : 365;
}
int number_of_days_till_now(int day,int mon,int year)
{
int i,sum=0;
for (i=0;i<mon-1;i++)
{
sum+= months_of_year;
}
sum+= day;
if(is_leap_year(year) && mon>2)
sum++;
return sum;
}
int Difference_Days(std::string s1,std::string s2){
char * pch;
int d1,m1,y1,d2,m2,y2;
std::string token, text(s1);
std::istringstream iss(text);
int flag=0;
while ( getline(iss, token, '-') ){
if(flag ==0)d1=StrToInt(token);
if(flag ==1)m1=StrToInt(token);
if(flag ==2)y1=StrToInt(token);
flag++;
}
std::string text2(s2);
std::istringstream iss2(text2);
flag=0;
while ( getline(iss2, token, '-') ){
if(flag ==0)d2=StrToInt(token);
if(flag ==1)m2=StrToInt(token);
if(flag ==2)y2=StrToInt(token);
flag++;
}
int first=number_of_days_till_now(d1,m1,y1);
int secont=number_of_days_till_now(d2,m2,y2);
if(y1==y2)return secont-first+1;
return secont-first+1+number_of_days_in_the_year(y1);
}
int main()
{
std::string sa1 ="01-01-2012";
std::string sa2 ="28-1-2012";
std::cout<<"Exoyn diafora "<<sa1<<" me tin "<<sa2<<" "<<Difference_Days(sa1,sa2)<<" days\n";
std::cout<<"\n";
system("pause");
return 0;
}