int judge(int year){
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
int yearsum(int year){
return year/4-year/100+year/400;
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
int yearsum(int year){
return year/4-year/100+year/400;
}
求两个日期间的天数(来自某大神的代码)
- /**
- 参见msdn tm time_t
- 注意有效范围,里面的year不能太早,否则计算不准确
- */
- int day_distance_1(int year1,int month1,int day1,int year2,int month2,int day2)
- {
- struct tm tm1;
- tm1.tm_year = year1 - 1900;
- tm1.tm_mon = month1 - 1;
- tm1.tm_mday = day1;
- tm1.tm_hour = 0;
- tm1.tm_min = 0;
- tm1.tm_sec = 0;
- struct tm tm2;
- tm2.tm_year = year2 - 1900;
- tm2.tm_mon = month2 - 1;
- tm2.tm_mday = day2;
- tm2.tm_hour = 0;
- tm2.tm_min = 0;
- tm2.tm_sec = 0;
- time_t time1;
- time_t time2;
- time1 = mktime(&tm1);
- time2 = mktime(&tm2);
- double diff = difftime(time1,time2);
- return (int)(diff/(3600*24));
- }
- /**
- 这个方法的计算范围很大,但是不清楚里面的算法内容,杯具...
- */
- int day_distance_2(int year1,int month1,int day1,int year2,int month2,int day2)
- {
- int nd, nm, ny; //new_day, new_month, new_year
- int od, om, oy; //old_day, oldmonth, old_year
- nm = (month2 + 9) % 12;
- ny = year2 - nm/10;
- nd = 365*ny + ny/4 - ny/100 + ny/400 + (nm*306 + 5)/10 + (day2 - 1);
- om = (month1 + 9) % 12;
- oy = year1 - om/10;
- od = 365*oy + oy/4 - oy/100 + oy/400 + (om*306 + 5)/10 + (day1 - 1);
- return od - nd;
- }