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

0
6060

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.

[nextpage title=”Program” ]

[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);

}

[/nextpage]

[nextpage title=”Output” ]

[/message_box]

[message_box title=”Output” color=”green”]

Enter year:1996

Enter first date : (day and month)

21  1

Enter second date : (day and month)

20  6

Total days : 151

[/message_box]

[/nextpage]

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments