计算日期差(C/C++实现)

不使用API,也不使用 Excel 以及计算器,计算某一个日期到另一个日期经过了多少天。如果考虑两个日期之间的关系,会非常麻烦,而选取一个日期基点,计算两个日期到基点经过的天数,然后作差会使问题简便。


代码

#include <stdio.h>
#include <stdlib.h>

bool isLeapYear(int year)
{
	return ((year%4==0 && year%100!=0) || year%400==0);
}
// 以公元 1 年 1 月 1 日为基准,计算经过的日期 
int getDays(int year, int month, int day)
{
	int m[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if(isLeapYear(year))
		m[2]++;
	int result = 0;
	for(int i = 1;i < year;i++)
	{
		result += 365;
		if(isLeapYear(i))
			result ++;
	}
	for(int i = 1;i < month;i++)
	{
		result += m[i];
	}
	result += day;
	
	return result;
}
int dayDis (int year1, int month1, int day1,
			int year2, int month2, int day2)
{
	return abs(getDays(year2, month2, day2) - getDays(year1, month1, day1));
}
	
int main(void)
{
	printf("%d\n",dayDis(2012, 9, 1, 2018, 3, 25));
	
	return 0;
} 


  • 12
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用 C 语言实现日期加减的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Date { int year; int month; int day; }; // 计算一个月有多少天 int days_in_month(int year, int month) { switch (month) { case 2: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 29; } else { return 28; } case 4: case 6: case 9: case 11: return 30; default: return 31; } } // 计算日期加上指定天数后的日期 struct Date add_days(struct Date date, int days) { while (days > 0) { int days_in_month = days_in_month(date.year, date.month); if (date.day + days > days_in_month) { days -= days_in_month - date.day + 1; date.day = 1; if (date.month == 12) { date.year++; date.month = 1; } else { date.month++; } } else { date.day += days; break; } } return date; } // 计算日期减去指定天数后的日期 struct Date sub_days(struct Date date, int days) { while (days > 0) { if (date.day - days < 1) { days -= date.day - 1; if (date.month == 1) { date.year--; date.month = 12; } else { date.month--; } date.day = days_in_month(date.year, date.month); } else { date.day -= days; break; } } return date; } int main() { struct Date date = {2021, 3, 15}; printf("原始日期:%d年%d月%d日\n", date.year, date.month, date.day); struct Date new_date = add_days(date, 10); printf("加上10天后的日期:%d年%d月%d日\n", new_date.year, new_date.month, new_date.day); new_date = sub_days(date, 10); printf("减去10天后的日期:%d年%d月%d日\n", new_date.year, new_date.month, new_date.day); return 0; } ``` 在这个示例代码中,我们定义了一个 `struct Date` 结构体来表示日期,其中包含年、月、日三个字段。 我们使用 `days_in_month` 函数来计算一个月有多少天。这个函数会根据年份和月份来判断每个月有多少天,考虑闰年和平年的情况。 我们使用 `add_days` 函数来计算日期加上指定天数后的日期。这个函数会循环遍历每一天,直到指定天数减为 0,每次循环都会判断当前月份是否已经超过了这个月的最大天数,如果超过了,就将月份加 1,将日期重置为 1。如果月份已经是 12 月了,就将年份加 1,月份重置为 1。 我们使用 `sub_days` 函数来计算日期减去指定天数后的日期。这个函数的实现和 `add_days` 函数类似,只是每次循环都会判断当前日期是否已经小于 1,如果小于 1,就将月份减 1,将日期重置为上个月的最大天数。如果月份已经是 1 月了,就将年份减 1,月份重置为 12。 在 `main` 函数中,我们定义了一个初始日期,然后分别调用 `add_days` 和 `sub_days` 函数来计算加上和减去指定天数后的日期,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值