Computes the fraction of the year between two serial dates, <span align="right" style="background-color:99FF99"><strong>Excel: YEARFRAC</strong></span>

You're viewing an older version of this page (#490). View the current version.

View versions (4)

Interface

#include <codecogs/finance/banking/yearfraction.h>

using namespace Finance::Banking;

Returns a number between 0 and 1 (normally), which represents the fraction of the year between two serial Julian dates:

Year\ Fraction = \frac{Day\ difference}{Number\ days\ in\ year}
(1)

If there is more than 1 year between the dates, then this value will be greater than 1.

The European or USA 360-day accounting system (with 30 days months - see dateDiff360), applies the following transformation to the start and end dates. \copydoc date/constants dateDiffType

Example 1

#include <stdio.h>

#include <codecogs/units/date/date.h>
#include <codecogs/units/date/isleapyear.h>
#include <codecogs/finance/banking/yearfraction.h>

using namespace Units::Date;
using namespace Finance::Banking;
int main()
{
  printf("\n StartDate    EndDate   Basis: 0 (USA)  1        2        3       4 (European)");
  int adate=date("25 Dec 2003");
  for(int i=0;i<200;i+=20)
  {
    int d,m,y;
    dateYMD(adate, y, m, d);
    printf("\n %2d-%2d-%d", d,m,y);
    dateYMD(adate+i, y, m, d);
    printf("  %2d-%2d-%d", d,m,y);
    
    printf("       %5.3lf    %5.3lf    %5.3lf    %5.3lf   %5.3lf",
           yearFraction(adate, adate+i, yb_USA),   // USA
           yearFraction(adate, adate+i, yb_Act),    // Actual/Actual
           yearFraction(adate, adate+i, yb_Act360),   // Actual/360
           yearFraction(adate, adate+i, yb_Act365),   // Actual/365
           yearFraction(adate, adate+i, yb_EU));    // European
  }
  return 0;
}

Output:

StartDate    EndDate   Basis: 0 (USA)  1        2        3       4 (European)
25-12-2003  25-12-2003       0.000    0.000    0.000    0.000   0.000
25-12-2003  14- 1-2004       0.053    0.055    0.056    0.055   0.053
25-12-2003   3- 2-2004       0.106    0.110    0.111    0.110   0.106
25-12-2003  23- 2-2004       0.161    0.164    0.167    0.164   0.161
25-12-2003  14- 3-2004       0.219    0.219    0.222    0.219   0.219
25-12-2003   3- 4-2004       0.272    0.273    0.278    0.274   0.272
25-12-2003  23- 4-2004       0.328    0.328    0.333    0.329   0.328
25-12-2003  13- 5-2004       0.383    0.383    0.389    0.384   0.383
25-12-2003   2- 6-2004       0.436    0.437    0.444    0.438   0.436
25-12-2003  22- 6-2004       0.492    0.492    0.500    0.493   0.492

Parameters

startDate
and...
endDate
are serial Julian dates (see date). Note the return value is not signed, so the two dates can be swapped.
basis
is the day counting system to use:
Blank

\copydoc YearBasis

GPL Licence — free for non commercial use. See Licence details.