C语言获取两个日期之间共有多少天

发布时间:2020-07-30 22:37:10 作者:Lee_1985
来源:网络 阅读:274

最近做项目需要基于C语言实现一个获取两个日期之间共有多少天的函数,通过搜索一些资料,实现了int GetInternalDays(Date date1, Date date2)接口,该接口实现了data1和date2直间相差的天数。


/*****************************************************************************************
 * Copyright:Yue Workstation
 *
 * FileName:CountDate.c
 *
 * Function:count the days of two date
 *
 * Author:Abel Lee
 *
 * Created on:2011-4-18
 *
 * Log:
 * ***************************************************************************************
 *         2011-4-18 create.
 *****************************************************************************************/
#include <stdio.h>
#define TestMain main  //The toggle of main function
typedef struct date
{
    int year;
    int month;
    int day;
}Date;//Date struct
//The month of the date given have days in this year.
int daysInMonth[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
/*****************************************************************************************
 * Function Name:IsLeap
 *
 * Function:Whether the year is a Leap
 *
 * Input Parameter:year:The year need to be judged
 *
 * Return Value:1:Leap 0:No leap
 *
 * Author: Abel Lee
 *
 * Log:create on 2011-4-18
 ****************************************************************************************/
int IsLeap(const int year)
{
    return (((year % 4 == 0) && year % 100 != 0) || year % 400 == 0);
}
/****************************************************************************************
 * Function Name:CountDays
 *
 * Function:Count the days of in this year
 *
 * Input Parameter:CurrentDate:the day of in this year
 *
 * Return Value:The days of in this year
 *
 * Author: Abel Lee
 *
 * Log:create on 2011-4-18
 ****************************************************************************************/
int CountDays(const Date *currentDate)
{
    int sumDays = daysInMonth[currentDate->month] + currentDate->day;
    if(IsLeap(currentDate->year) && (currentDate->month >= 3))
    {
        sumDays++;
    }
    return sumDays;
}
/***************************************************************************************
 * Function Name:SwapDate
 *
 * Function:Swap the two dates
 *
 * Input Parameter:date1:The first date
 *                 date2:The second date
 * Return Value:None
 *
 * Author:Li
 *
 * Log:create on 2011-4-19
 **************************************************************************************/
void SwapDate(Date *date1,Date *date2)
{
    int tmp = 0;
    tmp = date1->year;
    date1->year = date2->year;
    date2->year = tmp;
    tmp = date1->month;
    date1->month = date2->month;
    date2->month = tmp;
    tmp = date1->day;
    date1->day = date2->day;
    date2->day = tmp;
    return;
}
/*************************************************************************************
 * Function Name:GetIntervalDays
 *
 * Function:Get the days between date1 and date2
 *
 * Input Parameter:date1:The first date
 *                 date2:The second date
 *
 * Return Value:The days between date1 and date2
 *              -1 is the date format is error
 *
 * Author:Abel Lee
 *
 * Log:create on 2011-4-19
 ************************************************************************************/
int GetIntervalDays(Date *date1,Date *date2)
{
    int days1 = 0;
    int days2 = 0;
    int sumDays = 0;
    int i = 0;
    int years = 0;
    //Parameter range check
    if(date1->year < 1900 || date1->year > 2038 || date2->year < 1900 || date2->year > 2038)
    {
        printf("The year range is 1900 ~ 2038/n");
        return -1;
    }
    if(date1->month > 12 || date1->month < 1 || date2->month > 12 || date2->month < 1)
    {
        printf("The month range is 1 ~ 12/n");
        return -1;
    }
    switch(date1->month)
    {
    case 2:
        if(IsLeap(date1->year))
        {
            if(date1->day < 1 || date1->day > 29)
            {
                printf("%d-%d range is 0 ~ 29/n",date1->year,date1->month);
                return -1;
            }
        }
        else
        {
            if(date1->day < 1 || date1->day > 28)
            {
                printf("%d-%d range is 0 ~ 28/n",date1->year,date1->month);
                return -1;
            }
        }
        break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if(date1->day < 1 || date1->day > 31)
        {
            printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
            return -1;
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if(date1->day < 1 || date1->day > 30)
        {
            printf("%d-%d range is 0 ~ 30/n",date1->year,date1->month);
            return -1;
        }
        break;
    default:
        printf("The format of date is error!/n");
        return -1;
    }
    switch(date2->month)
    {
    case 2:
        if(IsLeap(date2->year))
        {
            if(date2->day < 1 || date2->day > 29)
            {
                printf("%d-%d range is 0 ~ 29/n",date2->year,date2->month);
                return -1;
            }
        }
        else
        {
            if(date2->day < 1 || date2->day > 28)
            {
                printf("%d-%d range is 0 ~ 28/n",date2->year,date2->month);
                return -1;
            }
        }
        break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if(date2->day < 1 || date2->day > 31)
        {
            printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
            return -1;            
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if(date2->day < 1 || date2->day > 30)
        {
            printf("%d-%d range is 0 ~ 30/n",date2->year,date2->month);
            return -1;
        }
        break;
    default:
        printf("The format of date is error!/n");
        return -1;
    }
    //date2 must be newer than date1
    if(date1->year > date2->year)
    {
        SwapDate(date1,date2);
    }
    else if((date2->year == date1->year) && (date1->month > date2->month))
    {
        SwapDate(date1,date2);
    }
    else if((date2->year == date1->year) && (date1->month == date2->month) && (date1->day > date2->day))
    {
        SwapDate(date1,date2);
    }
    years = date2->year - date1->year;
    days1 = CountDays(date1);
    days2 = CountDays(date2);
    sumDays = days2 - days1;
    if(years == 1)
    {
        sumDays += 365;
    }
    else if(years > 1)
    {
        for(i = date1->year; i < date2->year; i++)
        {
            sumDays += 365;
            if(IsLeap(i))
            {
                sumDays++;
            }
        }
    }
    return sumDays;
}
int main(int argc,char *argv[])
{
    Date date1,date2;
    int year,month,day;
    printf("input the first date(eg. 2011 2 5):");
    scanf("%d%d%d",&year,&month,&day);
    date1.year = year;
    date1.month = month;
    date1.day = day;
    printf("input the second date(eg. 2015 9 10):");
    scanf("%d%d%d",&year,&month,&day);
    date2.year = year;
    date2.month = month;
    date2.day = day;
    printf("********%d********\n",GetIntervalDays(&date1,&date2));
    printf("Application run successfully!\n");
    return 0;
}

可见,一个如此简单的功能,C语言却需要这么多代码,相比与Java/Python等更高级语言,确实开发效率相对较低!

推荐阅读:
  1. UNIX下获取前一天后一天的日期
  2. PHP&获取两个时间日期之间得所有日期

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言 日期 天数

上一篇:Linux下php安装redis扩展

下一篇: MariaDB、MySQL数据库主从同步

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》