Number of days between two date:
I have written a job which will give you an output of
number of months and remaining days between two date.
static void Ab_DateDifference(Args _args)
{
int Sttotdays, diffdays, EndTotdays, Enddiffdays;
int i, day, d1, mth, yr;
date middate;
int stleapyr, frmleapyr,toleapyr, leapyrs;
date StartDate = str2Date("2/13/2014", 213);
date EndDate = str2Date("3/29/2019", 213);
int noOfMonths,noOfMonthsBetweenYears, noofdays;
;
if (dayOfMth(EndDate) >= dayOfMth(StartDate))
{
noofdays = dayOfMth(EndDate) - dayOfMth(StartDate);
if (mthOfYr(StartDate) > 2)
{
frmleapyr = year(nextYr(StartDate));
toleapyr = year(EndDate);
}
else
{
frmleapyr = year(StartDate);
toleapyr = year(EndDate);
}
for ( i = frmleapyr ; i <= toleapyr ; i++)
{
if((i mod 4) == 0)
{
leapyrs = leapyrs + 1;
}
}
if (noofdays == 31)
{
noofdays = 0;
noOfMonthsBetweenYears = noOfMonthsBetweenYears + 1;
}
noofdays = noofdays + leapyrs;
noOfMonthsBetweenYears = intvNo(EndDate, StartDate, intvScale::YearMonth);
}
else
{
day = dayOfMth(EndDate);
mth = mthOfYr(EndDate);
yr = year(EndDate);
d1 = dayOfMth(StartDate);
middate = mkDate(d1, mth-1, yr);
noOfMonthsBetweenYears = intvNo(middate, StartDate, intvScale::YearMonth);
switch (mthOfYr(middate))
{
case 1: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 2: stleapyr = year(middate);
if ((stleapyr mod 4) == 0 && !((stleapyr mod 100) == 0))
{
Sttotdays = 29;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
}
else if ((stleapyr mod 400) == 0)
{
Sttotdays = 29;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
}
else
{
Sttotdays = 28;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
}
break;
case 3: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 4: Sttotdays = 30;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 5: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 6: Sttotdays = 30;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 7: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 8: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 9: Sttotdays = 30;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 10: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 11: Sttotdays = 30;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
case 12: Sttotdays = 31;
diffdays = Sttotdays + 1 - dayOfMth(middate);
noofdays = diffdays + dayOfMth(EndDate);
break;
}
if (noofdays == 31)
{
noofdays = 0;
noOfMonthsBetweenYears = noOfMonthsBetweenYears + 1;
}
}
info(strFmt("noOfMonthsBetweenYears : %1 noofdays: %2", int2str(noOfMonthsBetweenYears), noofdays));
}