比较两天时,我没有得到正确的日期。在这里,对于 dd=14,mm=8,yy=2014
time_t t1, t2;
struct tm my_target_date;
my_target_date.tm_sec = 0;
my_target_date.tm_min = 0;
my_target_date.tm_hour = 0;
my_target_date.tm_mday = 14;
my_target_date.tm_mon = 8;
my_target_date.tm_year = 112; /* Date today */
t1 = mktime (&my_target_date);
t2 = time (NULL);
sprintf (sbuff2,"Number of days since target date : %ld\n", (t2 - t1) / 86400);
最佳答案
在struct tm
中,
int tm_mday; // day of the month — [1, 31]
int tm_mon; // months since January — [0, 11]
tm_mday
从 1
开始,但是 tm_mon
从 0
开始。因此 my_target_date.tm_mon = 8;
实际上是九月,你还有一个月的假期。
关于c - 获取两个日期之间的天数差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25304982/