i just want to ask if you know a formula, or just VERY SIMPLE codes on how to compute days between two dates? i really don't have an idea on how to make it. thank you!

Recommended Answers

All 6 Replies

Whether it's simple or not depends on how well you know C, but I think the following is trivial:

#include <stdio.h>
#include <time.h>

int main(void)
{
    struct tm d1 = {0}, d2 = {0};

    d1.tm_year = 2012 - 1900;
    d1.tm_mon = 7;
    d1.tm_mday = 18;

    d2.tm_year = 2011 - 1900;
    d2.tm_mon = 7;
    d2.tm_mday = 18;

    printf("%f\n", difftime(mktime(&d1), mktime(&d2)) / 86400);

    return 0;
}

Try following Code :

int count=0;
int main()
{
int day1,day2,month1,month2,year1,year2,day11,month11,year11,year,month,day;
printf("Enter the privous date (DD) \n");
scanf("%d",&day1);
printf("Enter the privous month (MM) \n");
scanf("%d",&month1);
printf("Enter the privous year,for not being ambiguous input plz enter in YYYY format,else it will be a y2k bug\n");
scanf("%d",&year1);
printf("Enter the later date (DD)\n");
scanf("%d",&day2);
printf("Enter the later month (DD)\n");
scanf("%d",&month2);
printf("Enter the later year,for not being ambiguous input plz enter in YYYY format,else it will be a y2k bug\n");
scanf("%d",&year2);
day11=day1;
month11=month1;
year11=year1;
for(year=year11;year<=year2;year++)
{
for(month=month11;month<=12;month++)
{
if(year==year2 && month==month2)
{
countday(day11,day2);
break;
} 
else
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) //These are the months having 31 days
{ 
countday(day11,31); 
}
else if(month==4 || month==6 || month==9 || month==11) //These are the months having 30 days
{ 
countday(day11,30); 
}
else if(month==2) //This is for february only
{
if((year%400==0) || (year%4==0 && year%100!=0)) //This is for checking a leap year
{
countday(day11,29); 
}
else
{
countday(day11,28);
}
} 
day11=1; 
}
}
month11=1;
}
printf("The total number of days are : %d \n",count);
return 0;
}

//This function will start from the previous date and keep on counting each day as long as it come to the later date.
void countday(int ddin, int ddfnl)
{
int i;
for(i=ddin;i<=ddfnl;i++)
{
count++;
}
}

Alternatively,

L = 30 + { [ M + floor(M/8) ] MOD 2 }, where L is the month length in days and M is the month number 1 to 12. The expression is valid for all 12 months, but for M = 2 (February) adjust by subtracting 2 and then if it is a leap year add 1.

-Wikipedia (Gregorian Calendar)

Thank you for your posts! i'll try it all.

FWIW (For Whatever It's Worth), any serious software will convert date+time values into julian values when doing date/time arithmetic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.