碰到要计算两个日期之间是多少天是最麻烦的问题,所以写了这个函数,将就看看。需要注意的是,这个函数中没有把开始的那一天算在里面,如果需要请自行加1。

编辑平台是Arch Linux,gcc 4.5.0编译通过,执行正常,简单测试结果正确。

#include <stdlib.h>
#include <math.h>
#define LEAP 1
#define UNLEAP 0

// daycounter.h
// includes the following functions:
//    int daycounter(struct date date1,struct date date2)
//    int same_year(struct date date1,struct date date2)
//    int diff_year(struct date date1,struct date date2)
//    int leap(int year)
//    int month_day(struct date month_day)
//    int day_last(struct date date1)
//    int day_past(struct date date2)
// Writer infomation:
//      By:     陈志东
//      From:   南京工业大学
//      Email:  njutczd@gmail.com

//define a struct
struct date{
    int y,m,d;
};

//test if the year is a leap year
int leap (int year){
    if (year%4 == 0 && year%100 != 0 || year%400 == 0)
        return LEAP;
    else
        return UNLEAP;
}

//give days of a month, test the 2nd month
int month_day(struct date month_day){
    switch(month_day.m){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
            break;
        case 2:{
            if(leap(month_day.y))
                return 29;
            else
                return 28;
            }
            break;
        default:
            exit(1);
        }
}

//while date1 and date2 in different years, return days from date1 to the end of that year
int day_last(struct date date1){
    int a=0,b=0;
    struct date i;
    a=month_day(date1)-date1.d;
    for(i.y=date1.y,i.m=date1.m+1,i.d=date1.d;i.m<=12;i.m++)
        b+=month_day(i);
    return a+b;
}

//while date1 and date2 in different years, return days from the beginning of that year to date2
int day_past(struct date date2){
    int c=0,d=0;
    struct date i;
    c=date2.d;
    for(i.y=date2.y,i.m=1,i.d=1;i.m<=date2.m-1;i.m++)
        d+=month_day(i);
    return c+d;
}

//if date1 and date2 are in different years, use this fuction
int diff_year(struct date date1, struct date date2){
    int i,m=0,tmp;
    for(i=date1.y+1;i<=date2.y-1;i++){
        if(leap(i))
            tmp=366;
        else
            tmp=365;
        m+=tmp;
    }
    return day_last(date1)+m+day_past(date2);
}

//if date1 and date2 are in same year, use this fuction
int same_year(struct date date1,struct date date2){
    if(date1.m==date2.m)
        return date2.d-date1.d;
    else if(date1.m<date2.m){
        int k=0;
        struct date i;
        for(i.y=date1.y,i.m=date1.m+1,i.d=date1.d;i.m<=date2.m-1;i.m++)
            k+=month_day(i);
        return month_day(date1)-date1.d+k+date2.d;
    }
    else
        exit(1);//month of date1 is later than month of date2
}

//fuction to test if date1 and date2 are in same year and use the right fuction
int daycounter(struct date date1,struct date date2){
    if(date1.y==date2.y)
        return same_year(date1,date2);
    else if(date1.y<date2.y)
        return diff_year(date1,date2);
    else
        exit(1);//year of date1 is later than year of date2
}

下面是测试函数,调用daycounter.h中的int daycounter(struct date date1,struct date date2)函数计算两个日期间的天数。

#include <stdio.h>
#include "daycounter.h"

//daycounter.c
//调用daycounter.h中的int daycounter(struct date date1,struct date date2)函数计算两个日期间的天数

int main(){
    struct date date1,date2;
    printf("From date (xxxx.xx.xx):\t");
    scanf("%d.%d.%d",&date1.y,&date1.m,&date1.d);
    printf("to date (xxxx.xx.xx):\t");
    scanf("%d.%d.%d",&date2.y,&date2.m,&date2.d);
    //the dates entered should be test here to see if they are legal
    if((date1.m < 1 || date1.m > 12) ||(date2.m < 1 || date2.m > 12)){
        printf("Month should between 1 to 12!\n");
        return 1;
    }
    if((date1.d <1 || date1.d > month_day(date1)) || (date2.d <1 || date2.d > month_day(date2))){
        printf("Please pay attention to the date you input!\n");
        return 1;
    }
    if(date1.y>date2.y){
        printf("The beginning year should before the ending year!\n");
        return 1;
    }
    printf("Result is %d\n",daycounter(date1,date2));
    return 0;
}

如果您发现算法中有错误,请留言指正,谢谢!