# 计算时间差【C++】

``````2
20130527193000 20130527180000
20131231235959 20130101000000``````

``````0 5400
364 86399``````

``````#include <iostream>
#include <string>
using namespace std;

class DateTime {

public:

int year;

int month;

int day;

int hour;

int minute;

int seconds;

public:

void TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds);

//对t1 ,t2 表示的两个时间计算时间差值,将相差的天数保存在days中,将相差的秒数保存在seconds中

};

void DateTime::TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds)
{

int i;
int days1,days2;
int seconds1,seconds2;
days1=0,days2=0;
seconds1=0,seconds2=0;

for(i=2000;i<t1.year;i++)
{
if((i%4==0&&i%100!=0)||i%400==0)
days1+=366;
else
days1+=365;
}
for(i=1;i<t1.month;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days1+=31;break;
case 4:
case 6:
case 9:
case 11:days1+=30;break;
case 2:
{
if((t1.year%4==0&&t1.year%100!=0)||t1.year%400==0)
days1+=29;
else
days1+=28;
};break;
}
}
days1+=t1.day;

for(i=2000;i<t2.year;i++)
{
if((i%4==0&&i%100!=0)||i%400==0)
days2+=366;
else
days2+=365;
}
for(i=1;i<t2.month;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days2+=31;break;
case 4:
case 6:
case 9:
case 11:days2+=30;break;
case 2:
{
if((t2.year%4==0&&t2.year%100!=0)||t2.year%400==0)
days2+=29;
else
days2+=28;
};break;
}
}
days2+=t2.day;

days=days2-days1;
if(days<0)
days=0-days;

for(i=0;i<t1.hour;i++)
seconds1+=(60*60);
for(i=0;i<t1.minute;i++)
seconds1+=60;
seconds1+=t1.seconds;

for(i=0;i<t2.hour;i++)
seconds2+=(60*60);
for(i=0;i<t2.minute;i++)
seconds2+=60;
seconds2+=t2.seconds;

seconds=seconds2-seconds1;
if(seconds<0)
seconds=0-seconds;
}

int main()
{

DateTime a1,a2;
//   string c,d;
char c[15],d[15];
int n;
int dayss,secondss;
cin>>n;
for(;n>0;n--)
{
cin>>c;
cin>>d;
a1.year=(c[0]-'0')*1000+(c[1]-'0')*100+(c[2]-'0')*10+c[3]-'0';
a1.month=(c[4]-'0')*10+(c[5]-'0');
a1.day=(c[6]-'0')*10+(c[7]-'0');
a1.hour=(c[8]-'0')*10+(c[9]-'0');
a1.minute=(c[10]-'0')*10+(c[11]-'0');
a1.seconds=(c[12]-'0')*10+(c[13]-'0');

a2.year=(d[0]-'0')*1000+(d[1]-'0')*100+(d[2]-'0')*10+(d[3]-'0');
a2.month=(d[4]-'0')*10+(d[5]-'0');
a2.day=(d[6]-'0')*10+(d[7]-'0');
a2.hour=(d[8]-'0')*10+(d[9]-'0');
a2.minute=(d[10]-'0')*10+(d[11]-'0');
a2.seconds=(d[12]-'0')*10+(d[13]-'0');

//       cout<<a1.year<<" "<<a1.month<<" "<<a1.day<<" "<<a1.hour<<" "<<a1.minute<<" "<<a1.seconds<<endl;
//		cout<<a2.year<<" "<<a2.month<<" "<<a2.day<<" "<<a2.hour<<" "<<a2.minute<<" "<<a2.seconds<<endl;
a1.TimeDiff(a1,a2,dayss,secondss);
cout<<dayss<<" "<<secondss<<endl;
}
return 0;
}
``````

``````#include<stdio.h>
#include <iostream>
using namespace std;

class DateTime {

public:

int year;

int month;

int day;

int hour;

int minute;

int seconds;

public:

void TimeDiff(DateTime &t1,DateTime &t2,int &days,int &seconds);

//对t1 ,t2 表示的两个时间计算时间差值,将相差的天数保存在days中,将相差的秒数保存在seconds中
void display(void);
};
void DateTime::display(void)
{
cout<<year<<' '<<month<<' '<<day<<' '<<hour<<' '<<minute<<' '<<seconds<<endl;
}
void DateTime::TimeDiff(DateTime& t1,DateTime& t2,int& days,int& seconds)
{
bool run(DateTime);
int run_day(DateTime&);
int norun_day(DateTime&);
if(t1.hour==t2.hour && t1.minute==t2.minute){
if(t1.seconds>=t2.seconds){
seconds=t1.seconds-t2.seconds;
}
else{
seconds=t2.seconds-t1.seconds;
}
}
else if(t1.hour==t2.hour){
seconds=0;
int s1,s2;
if(t1.minute>=t2.minute){
s1=t1.seconds;
s2=60-t2.seconds;
seconds=(t1.minute-t2.minute-1)*60+s1+s2;
}
else{
s2=t2.seconds;
s1=60-t1.seconds;
seconds=(t2.minute-t1.minute-1)*60+s1+s2;
}
}
else{
seconds=0;
int s1,s2;
if(t1.hour>t2.hour){
s1=t1.minute*60+t1.seconds;
s2=3600-t2.minute*60-t2.seconds;
seconds=(t1.hour-t2.hour-1)*3600+s1+s2;
}
else{
s2=t2.minute*60+t2.seconds;
s1=3600-t1.minute*60-t1.seconds;
seconds=(t2.hour-t1.hour-1)*3600+s1+s2;
}
}

if(t1.year==t2.year && t1.month==t2.month){
if(t1.day>=t2.day)
days=t1.day-t2.day;
else
days=t2.day-t1.day;
}
else if(t1.year==t2.year){
if(run(t1)){  //如果是闰年
int d1,d2;
d1=run_day(t1);
d2=run_day(t2);
if(d1>=d2)
days=d1-d2;
else
days=d2-d1;
}
else{   //不是闰年
int d1,d2;
d1=norun_day(t1);
d2=norun_day(t2);
if(d1>=d2)
days=d1-d2;
else
days=d2-d1;
}
}
else{   //年月日都不相同的情况
DateTime y1,y2;
if(t1.year>=t2.year){
y1=t1;
y2=t2;
}
else{
y1=t2;
t2=t1;
}
//保持y1>y2
int sum=0;
if(run(y1))
sum+=run_day(t1);
else
sum+=norun_day(t1);
y1.year-=1;
while(y1.year!=y2.year){
if(run(y1)){
sum+=366;
}
else
sum+=365;
y1.year--;
}
if(run(y1)){
sum=sum+(366-run_day(y2));
}
else{
sum=sum+(365-norun_day(y2));
}
}
}
bool run(DateTime t1)
{
bool f;
if(t1.year%100==0){
if(t1.year%400==0)
f=true;
else
f=false;
}
else{
if(t1.year%4==0)
f=true;
else
f=false;
}
return f;
}

int run_day(DateTime& t)
{
int d;
switch(t.month){
case 1:d=t.day;break;
case 2:d=31+t.day;break;
case 3:d=60+t.day;break;
case 4:d=91+t.day;break;
case 5:d=121+t.day;break;
case 6:d=152+t.day;break;
case 7:d=182+t.day;break;
case 8:d=213+t.day;break;
case 9:d=244+t.day;break;
case 10:d=274+t.day;break;
case 11:d=305+t.day;break;
case 12:d=335+t.day;break;
}
return d;
}
int norun_day(DateTime& t)
{
int d;
switch(t.month){
case 1:d=t.day;break;
case 2:d=31+t.day;break;
case 3:d=59+t.day;break;
case 4:d=90+t.day;break;
case 5:d=120+t.day;break;
case 6:d=151+t.day;break;
case 7:d=181+t.day;break;
case 8:d=212+t.day;break;
case 9:d=243+t.day;break;
case 10:d=273+t.day;break;
case 11:d=304+t.day;break;
case 12:d=334+t.day;break;
}
return d;
}
int main()
{
int T;
cin>>T;
while(T--){
DateTime t1,t2;
scanf("%4d%2d%2d%2d%2d%2d",&t1.year,&t1.month,&t1.day,&t1.hour,&t1.minute,&t1.seconds);
scanf("%4d%2d%2d%2d%2d%2d",&t2.year,&t2.month,&t2.day,&t2.hour,&t2.minute,&t2.seconds);
DateTime t3;
int days,seconds;
t3.TimeDiff(t1,t2,days,seconds);
cout<<days<<' '<<seconds<<endl;
}
}
``````