#include <string>
#include <sstream>
#include <iostream>
using namespace std;
//=======================================================//
void str2int(int &int_temp,const string &string_temp)
{
stringstream stream(string_temp);
stream>>int_temp;
}
//=======================================================//
int interval_date(string begin, string end)
{
string a1, a2, a3, b1, b2, b3;
a1+=begin[0];
a1+=begin[1];
a1+=begin[2];
a1+=begin[3];
a2+=begin[5];
a2+=begin[6];
a3+=begin[8];
a3+=begin[9];
b1+=end[0];
b1+=end[1];
b1+=end[2];
b1+=end[3];
b2+=end[5];
b2+=end[6];
b3+=end[8];
b3+=end[9];
cout<<"begin = "<<a1<<" "<<a2<<" "<<a3<<endl;
cout<<"end = "<<b1<<" "<<b2<<" "<<b3<<endl;
int int_a1, int_a2, int_a3, int_b1, int_b2, int_b3;
str2int(int_a1, a1);
str2int(int_a2, a2);
str2int(int_a3, a3);
str2int(int_b1, b1);
str2int(int_b2, b2);
str2int(int_b3, b3);
int interval_days;
interval_days = (int_b1-int_a1)*365 + (int_b2-int_a2)*30 + (int_b3-int_a3);
cout<<"interval_days = "<<interval_days<<endl;
return interval_days;
}
//=======================================================//
//=======================================================//
int main()
{
string a = "2018-07-12";
string b = "2019-07-12";
int interval = interval_date(a,b);
return 0;
}
运行结果:
begin = 2018 07 12
end = 2019 07 12
interval_days = 365