如何用c语言编辑计算日期差的程序

来源:百度知道 编辑:UC知道 时间:2024/06/18 00:27:37
计算2008年3月16日与2009年10月6日之间相差多少天

结果:
相差569天

#include <stdio.h>
#ifndef __SDATE__
#define __SDATE__
struct SDate {
short int year;
short int month;
short int day;
};
#endif

int IsLeapYear(short year);
int DaysOfOneYear(SDate sd);
int SDateToAbsDays(SDate sd);

int IsLeapYear(short year) {
return year%4==0&&year%100||year%400==0;
}

int DaysOfOneYear(SDate sd) {
switch(sd.month-1) {
case 11:
sd.day+=30;
case 10:
sd.day+=31;
case 9:
sd.day+=30;
case 8:
sd.day+=31;
case 7:
sd.day+=31;
case 6:
sd.day+=30;
case 5:
sd.day+=31;
case 4:
sd.day+=30;
case 3:
sd.day+=31;
case 2:
sd.day+=IsLeapYear(sd.year)?29:28;
case 1:
sd.day+=31;
}
return sd.day;
}

int SDateToAbsDays(SDate sd) {
int years = sd.year -1;
int days = years*365