user-0 提问: 求两个日期相差天数，求教哪里有错误

回复:
user: user-1
content: 有专门的公式，自己写应该闰年那块处理问题比较多

user: user-2
content: The COleDateTime class handles dates from 1 January 100 – 31 December 9999.

user: user-3
content: 有那么复杂吗？通过C标准函数就可以简单完成 1、将日期转换为距离1970的秒数 2、将两个秒数相减 3、将差值处于一天的秒数  不需要考虑闰年大小月等问题

user: user-4
content: #include <stdio.h> #include <time.h>  int main(void) {     time_t x, y;     int year, mon, day;     scanf("%d%d%d", &year, &mon, &day);     struct tm a = { 0, 0, 0, day, mon - 1, year - 1900 };     scanf("%d%d%d", &year, &mon, &day);     struct tm b = { 0, 0, 0, day, mon - 1, year - 1900 };     x = mktime(&a);     y = mktime(&b);     if ( x != (time_t)(-1) && y != (time_t)(-1) )     {         double difference = difftime(y, x) / (60 * 60 * 24);         printf("%d days\n", (int)difference);     }     return 0; }

user: user-0
content: 、、、、感谢说明

user: user-2
content: 仅供参考：#include <afxdisp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, TCHAR* argv[]) {     COleDateTime t;     COleDateTimeSpan ts;     CString s,fmt;     int nYear;     int nMonth;     int nDay;     int nHour;     int nMin;     int nSec;     int lDays;     int nHours;     int nMins;     int nSecs;     int i,N;     // initialize MFC and print and error on failure     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {         printf("Fatal Error: MFC initialization failed\n");         return 1;     }     if (argc<13) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 2;     }     if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";     else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";     else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";     else {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 3;     }     nYear =atoi(argv[ 1]);     nMonth=atoi(argv[ 2]);     nDay  =atoi(argv[ 3]);     nHour =atoi(argv[ 4]);     nMin  =atoi(argv[ 5]);     nSec  =atoi(argv[ 6]);     lDays =atoi(argv[ 7]);     nHours=atoi(argv[ 8]);     nMins =atoi(argv[ 9]);     nSecs =atoi(argv[10]);     N     =atoi(argv[11]);     if (N<=0) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 4;     }     t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);     ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );     for (i=1;i<=N;i++) {         s=t.Format(fmt);         printf("%08d %s\n",i,s);         t=t+ts;     }     return 0; }

user: user-5
content: 我这儿有个例子  #include <stdio.h> #include <stdlib.h> #include <Windows.h>  static int month_of_year [][13] = {     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };  struct Date {     int year;     int mon;     int day; };  BOOL is_leap_year (int year) {     if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400)         return TRUE;     return FALSE; }  BOOL date_early (const struct Date *x, const struct Date *y) {     if (x->year < y->year) {         return TRUE;     } else if (x->year == y->year && x->mon < y->mon) {         return TRUE;     } else if (x->year == y->year && x->mon == y->mon && x->day < y->day) {         return TRUE;     }      return FALSE; }  int date_day_of_year (const struct Date *x) {     int mon, days = 0;      for (mon = 1; mon < x->mon; ++mon) {         days += month_of_year[is_leap_year (x->year)][mon];     }      days += x->day;      return days; }  int date_compare_aux (const struct Date *x, const struct Date *y) {     int days = 0, year;     int xdays = date_day_of_year (x);     int ydays = date_day_of_year (y);      if (x->year == y->year) {         return ydays - xdays;     }      for (year = x->year + 1; year < y->year; ++year) {         days += (is_leap_year (year) ? 366 : 365);     }      if (is_leap_year (x->year)) {         return 366 - xdays + days + ydays;     } else {         return 365 - xdays + days + ydays;     } }  int date_compare (const struct Date *x, const struct Date *y) {     if (date_early (x, y))         return - date_compare_aux (x, y);     return date_compare_aux (y, x); }  int main () {     struct Date x = {2014, 1, 1};     struct Date y = {2012, 1, 1};      printf ("%d\n", date_compare (&x, &y)); }

user: user-6
content: 不良风格，毫无注释

user: user-7
content: c==c+1; 这样是不会赋值的吧？