C Program to compute the difference between two dates.

Write a C program to compute the difference between two dates.In this C Program, We check the date is valid or not and then calculating the No. of days of first date and second date from Jan 1 of first date, then subtract the smaller date from another. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first date day, month, yearn");
scanf("%d%d%d",&day1,&mon1,&year1);
printf("Enter second date day, month, yearn");
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("nn 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 Inputnnnn"); exit(1);
}
return(y);
}

Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

One comment on “C Program to compute the difference between two dates.

  • You are not taken the leap year extra day count in your dater() function.
    If u see clearly: If let say year (which is not the reference one…means greater one is a leap year)
    Try your code for dates: 16/05/2004 and 25/08/2012

    Also rather than using condition "i%4" use a proper Isleapyear check,
    For eg:
    int Isleap(int year) {
    if(((year % 4) == 0 && (year % 100) != 0) || (year % 400 == 0)) {
    return 1;
    }
    else {
    return 0;
    }
    }

    Reply

Leave a Reply