首页 > 代码库 > 时间差

时间差

#include<stdio.h>
int main()
{
    int n,y1,m1,d1,y2,m2,d2,c1,c2,i,j,k;
    int a[2][12]={{0,31,28,31,30,31,30,31,31,30,31,30},
                  {0,31,29,31,30,31,30,31,31,30,31,30}};
    scanf("%d",&n);
    for(k=1;k<=n;k++){
        scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
        c1=0;c2=0;
        for(i=1;i<y1;i++){
            if(i%4==0&&i%100!=0||i%400==0) c1+=366;
            else c1+=365;
        }
        for(i=1;i<m1;i++){
            if(y1%4==0&&y1%100!=0||y1%400==0) c1+=a[1][i];
            else c1+=a[0][i];
        }
        c1+=d1;
        for(i=1;i<y2;i++){
            if(i%4==0&&i%100!=0||i%400==0) c2+=366;
            else c2+=365;
        }
        for(i=1;i<m2;i++){
            if(y2%4==0&&y2%100!=0||y2%400==0) c2+=a[1][i];
            else c2+=a[0][i];
        }
        c2+=d2;
        printf("%d\n",c2-c1);
    }
    return 0; 
}

 

时间差