Programmer : Dhiren Patel
Platform : NT 4.0 Workstation
Compiler : VC++ 5.0
Date : April 21, 1998
Copyright : You are free to distribute this source
code provided this header is not removed and
not altered in any way without prior written
permission from the programmer.
*************************************************************************/
#include <stdio.h>
#include <conio.h>
/*************************************************************************
Function MDY
Parameters : Character Pointer to two date strings in MonthDayYear format.
Return Value : Returns an integer specifying the no of days between
two dates. If illegal dates are
entered, then a 0 is returned
Programmer : Dhiren Patel
Comment : Standard library calls are avoided so that
the function can be compiled and used in
any other languages withot requiring the libraries and
also to keep the function small.
Copyright : You are free to distribute this source
code provided this header is not removed and
not altered in any way without prior written permission
from the programmer.
****************************************************************************/
int MDY(char* ststr,char* edstr)
{
int i,flag = 0;
int days = 0;
int styr,stmth,stdt;
int edyr,edmth,eddt;
for(i=0; ststr[i] != '\0'; i++);
if(i != 8)
return 0;
for(i=0; edstr[i] != '\0'; i++);
if(i != 8)
return 0;
/*extracting dates from string */
styr = (ststr[4]-'0')*1000 + (ststr[5]-'0')*100 + (ststr[6]-'0')*10
+ (ststr[7]-'0');
stmth = (ststr[0]-'0')*10 + (ststr[1]-'0');
stdt = (ststr[2]-'0')*10 + (ststr[3]-'0');
edyr = (edstr[4]-'0')*1000 + (edstr[5]-'0')*100 + (edstr[6]-'0')*10
+ (edstr[7]-'0');
edmth = (edstr[0]-'0')*10 + (edstr[1]-'0');
eddt = (edstr[2]-'0')*10 + (edstr[3]-'0');
/*if same dates */
if (styr == edyr)
{
if( stmth == edmth)
{
if( stdt == eddt)
{
return 0;
}
else if(stdt > eddt)
{
i = stdt; stdt = eddt; eddt = i; flag = 1;
}
}
else if( stmth > edmth)
{
i = stmth; stmth = edmth; edmth = i; flag = 1;
i = stdt; stdt = eddt; eddt = i;
}
}
/*reverse the dates */
if (styr > edyr)
{
i = styr; styr = edyr; edyr = i; flag = 1;
i = stmth; stmth = edmth; edmth = i;
i = stdt; stdt = eddt; eddt = i;
}
/*checking for wromg date and year */
if( stmth > 12 || stmth < 1 || edmth > 12 || edmth <1 ||
stdt > 31 || stdt < 1 || eddt > 31 || eddt < 1)
return 0;
/*checking for wrong date in feb styr */
if( stmth == 2 )
if (stdt > 28)
if((styr % 4)!=0)
return 0;
else if(stdt != 29)
return 0;
/*checking for wrong date in feb edyr */
if( edmth == 2 )
if (eddt > 28)
if((edyr % 4)!=0)
return 0;
else if(eddt != 29)
return 0;
/*if same edyr & styr */
if(styr == edyr)
{
if(stmth == edmth)
{
days = days + (eddt - stdt);
if(flag) return -days; else return days;
}
else
{
switch(stmth) /* adding for stmth */
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + (31-stdt); break;
case 4:
case 6:
case 9:
case 11:
days =days + (30-stdt); break;
case 2:
if((styr % 4) == 0)
{
days = days + (29- stdt);
break;
}
else
{
days = days + (28-stdt);
break;
}
}
days = days + eddt; /* adding for edmth */
/* adding for months in between */
if((edmth - stmth) > 1)
{
for(i = stmth + 1; i < edmth; i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + 31; break;
case 4:
case 6:
case 9:
case 11:
days =days + 30; break;
case 2:
if((styr % 4) == 0)
{
days = days + 29;
break;
}
else
{
days = days + 28;
break;
}
}
}
}
}
if(flag) return -days; else return days;
}
/*adding for in between years */
if((edyr - styr) > 1)
for( i = styr+1; i <= edyr-1; i++)
if ((i % 4) == 0)
days = days + 366;
else
days = days + 365;
/*adding for months of start year */
for( i = stmth+1; i < 13; i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + 31; break;
case 4:
case 6:
case 9:
case 11:
days =days + 30; break;
case 2:
if((styr % 4) == 0)
{
days = days + 29;
break;
}
else
{
days = days + 28;
break;
}
}
}
/*adding for months of end year */
for( i = 1; i < edmth; i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + 31; break;
case 4:
case 6:
case 9:
case 11:
days = days + 30; break;
case 2:
if((edyr % 4) == 0)
{
days = days + 29;
break;
}
else
{
days = days + 28;
break;
}
}
}
/*adding for stmth */
switch(stmth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + (31 - stdt); break;
case 4:
case 6:
case 9:
case 11:
days = days + (30 - stdt); break;
case 2:
if((styr % 4) == 0)
{
days = days + (29 - stdt);
break;
}
else
{
days = days + (28 - stdt);
break;
}
}
/*adding for edmth */
days = days + eddt;
if(flag) return -days; else return days;
}
/* End of MDY function
Delete the code that follows below when compiling for the routine.
With the
below code it is a stand alone program for MDY */
void main()
{
char stdate[9];
char eddate[9];
printf("\nEnter start date : ");
scanf("%s",stdate);
printf("\nEnter end date : ");
scanf("%s",eddate);
printf("\n\nDays = %d\n",MDY(stdate,eddate));
_getch();
}