用结构体表示日期,输入一个日期(年、月、日),计算从输入年的1月1日到输入的日期的总天数days并输出。
struct tm when;
__time64_t now, result;
int days;
char buff[80];
time( &now );
_localtime64_s( &when, &now );
asctime_s( buff, sizeof(buff), &when );
printf( "Current time is %s\n", buff );
days = 20;
when.tm_mday = when.tm_mday + days;
result=mktime( &when ) ;
double d=difftime(result,now);
int nDays=d/60/60/24;
可以计算出结果差20天
time_t mktime(struct tm * timeptr);
其返回值就是转化后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作了,下面的例子可以计算出1997年7月1日是星期几:
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
struct tm t;
time_t t_of_day;
t.tm_year=1997-1900;
t.tm_mon=6;
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
return 0;
}
你不用自己设定结构体,tm就可以用,上面mktime就是组合成时间,然后你在计算这个日期到现在的间隔
difftime(ends,start)