日期加减

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值