Reputation: 104
I am trying to take one date away from another in C and find the difference between them in days. However, this is much more complicated than it first appeared to me as I obviously have to allow for differing days in different years due to leap years and a differing numbers of days depending on which month it is. I did try to use time.h but I do not think that it allows for leap years.
Currently my data is stored as integers in an array, for example, {2010, 5, 1, 2011, 6, 1}
.
So could someone please post or point me towards an algorithm that will help me achieve this task? Thank you very much.
Upvotes: 0
Views: 17619
Reputation: 172568
The standard library C Time Library contains structures and functions you want.
This header file contains definitions of functions to get and manipulate date and time information.
Also check C date and time functions and C program days between two dates
EDIT:-
Try this:
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);
}
or using time.h
try like this:
#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;
}
Upvotes: 5
Reputation: 131
/* 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;
}
}
Upvotes: -1
Reputation: 136475
Algo:
struct tm
.struct tm
to time_t
.time_t
s. It yields the difference between dates in seconds.Upvotes: 3