完整的程序:#include#include#include#include#includeint get_days(const char* from, const char* to);time_t convert(int year,int month,int day);int main(){const char* from="2013-3-15";const char* to="2015-8-14";int days=get_days(from,to);printf("From:%s\nTo:%s\n",from,to);printf("%d\n",days);system("pause");return 0;}time_t convert(int year,int month,int day){tm info={0};info.tm_year=year-1900;info.tm_mon=month-1;info.tm_mday=day;return mktime(&info);}int get_days(const char* from, const char* to){int year,month,day;sscanf(from,"%d-%d-%d",&year,&month,&day);int fromSecond=(int)convert(year,month,day);sscanf(to,"%d-%d-%d",&year,&month,&day);int toSecond=(int)convert(year,month,day);return (toSecond-fromSecond)/24/3600;}
运行结果可以看出2013-3-15到2015-8-14一共相隔882天。