# Find out difference in days between any two dates:C Program

0
826

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.

Current my data is stored as integers in array, for example,{21/1/1996 to 20/6/1996}

So could someone please post or point me towards an algorithm that will help me achieve this task?Thank you very much.

[message_box title=”Program” color=”blue”]

/*write a program to find out the difference in days between any two dates*/

/*Written by Utpal Chaudhary*/

/* Student of L.D COLLEGE OF ENGINEERING, Navrangpura, Ahmedabad-15 */

#include<stdio.h>

#include<conio.h>

void main( )

{

int month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; Â /* Here n=28 if year is not leap otherwise n=29 */

int i ,y ,d1, d2, m1, m2, total days=0;

printf (“Enter Year:”);

scanf (“%d”,&y);

if ( ( y%4 =0 Â && Â y%100!=0) || (y%400==0 ) ) month[1]=29;

printf (“Enter first date: (day and month )”);

scanf (“%d %d”,&d1,M1);

printf (“Enter Second date:(day and month)”);

scanf(“%d Â %d”,&d2,&m2);

totaldays=totaldays + month[m1-1]-d1;

for(i=m1, i<m2, i++)

{

totaldays=totaldays +month[i];

}

totaldays =Â totaldays + d2;

printf (“Total days = %d \n “,totaldays);

}