以下代码仅供参考学习!
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
int get_year(char* date_str)
{
char buf[5];
buf[0] = date_str[0];
buf[1] = date_str[1];
buf[2] = date_str[2];
buf[3] = date_str[3];
buf[4] = 0x00;
return (atoi(buf));
}
int get_mon(char* date_str)
{
char buf[3];
buf[0] = date_str[4];
buf[1] = date_str[5];
buf[2] = 0x00;
return (atoi(buf));
}
int get_day(char* date_str)
{
char buf[3];
buf[0] = date_str[6];
buf[1] = date_str[7];
buf[2] = 0x00;
return (atoi(buf));
}
//日期格式 yyyyMMdd
int day_com(char*beg_day/*开始日期*/,char*end_day/*结束日期*/)
{
int day;
int year;
int mon;
time_t t1,t2;
struct tm m1,m2;
time(&t1);
m1 = *localtime(&t1);
m2 = m1;
year = get_year(beg_day);
mon = get_mon(beg_day);
day = get_day(beg_day);
m1.tm_year = year-1900;
m1.tm_mon = year-1;
m1.tm_mday = day;
t1 = mktime(&m1);
year = get_year(end_day);
mon = get_mon(end_day);
day = get_day(end_day);
m2.tm_year = year-1900;
m2.tm_mon = year-1;
m2.tm_mday = day;
t2 = mktime(&m2);
return(int) ((t2-t1)/(24*3600));
}