我试图在C中与另一个约会一个日期,并在几天内找到它们之间的差异。然而,这比它最初出现的要复杂得多,因为我显然必须考虑到不同年份的不同日期,因为闰年和不同的天数取决于它是哪个月。我确实试过使用time.h但是我认为它不允许闰年。
目前,我的数据作为整数存储在数组中,例如{2010, 5, 1, 2011, 6, 1}
。
那么有人可以发帖或指点我的算法来帮助我完成这项任务吗?非常感谢你。
答案 0 :(得分:5)
标准库C Time Library包含您想要的结构和功能。
此头文件包含要获取和的函数的定义 操纵日期和时间信息。
同时检查C date and time functions和C program days between two dates
修改: - 强>
试试这个:
int main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first date day, month, year\n");
scanf("%d%d%d",&day1,&mon1,&year1);
printf("Enter second date day, month, year\n");
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2<year1)
ref = year2;
dd1=0;
dd1=dater(mon1);
for(i=ref;i<year1;i++)
{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
dd2=0;
for(i=ref;i<year2;i++)
{
if(i%4==0)
dd2+=1;
}
dd2=dater(mon2)+dd2+day2+((year2-ref)*365);
printf("\n\n Difference between the two dates is %d days",abs(dd2-dd1));
getch();
}
int dater(x)
{ int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Invalid Input\n\n\n\n"); exit(1);
}
return(y);
}
或使用time.h
尝试这样:
#include <stdio.h>
#include <time.h>
int main ()
{
struct tm start_date;
struct tm end_date;
time_t start_time, end_time;
double seconds;
start_date.tm_hour = 0; start_date.tm_min = 0; start_date.tm_sec = 0;
start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 113;
end_date.tm_hour = 0; end_date.tm_min = 0; end_date.tm_sec = 0;
end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 113;
start_time = mktime(&start_date);
end_time = mktime(&end_date);
seconds = difftime(end_time, start_time);
printf ("%.f seconds difference\n", seconds);
return 0;
}
答案 1 :(得分:3)
ALGO:
struct tm
。struct tm
转换为time_t
。time_t
之间的差异。它产生日期之间的差异,以秒为单位。答案 2 :(得分:-1)
/* This function calculates the differance between two dates, passes 6 parameters date-month-year of first date and date-month-year of second date and prints the difference in date-weeks-year format */
void Date::subtract(int firstDate, int firstMonth, int firstYear,int secondDate, int secondMonth, int secondYear)
{
/*check the dates are valid or not */
if(isDateValid(firstDate,firstMonth,firstYear) && isDateValid(secondDate,secondMonth,secondYear) )
{
firstMonth = (firstMonth + 9) % 12;
firstYear = firstYear - firstMonth / 10;
FirstNoOfDays = 365 * firstYear + firstYear/4 - firstYear/100 + firstYear/400 + (firstMonth * 306 + 5) /10 + ( firstDate - 1 );
secondMonth = (secondMonth + 9) % 12;
secondYear = secondYear - secondMonth / 10;
SecondNoOfDays = 365 * secondYear + secondYear/4 - secondYear/100 + secondYear/400 + (secondMonth * 306 + 5) /10 + ( secondDate - 1 );
dayDifference = abs(FirstNoOfDays - SecondNoOfDays); /* uses absolute if the first date is smaller so it wont give negative number */
years = dayDifference / 365;
weeks = (dayDifference % 365)/7;
days = (dayDifference % 365) % 7;
cout<<years<<" years "<<weeks<<" weeks "<<days<< " days"<<endl;
}
else
{
cout<<"Invalid Date"<<endl;
}
}