curatedapi

Excel YEARFRAC function: fraction of a year between dates

The fraction of a year between two dates under Excel's five day-count bases, including Excel's own actual/actual rule.

Start date.
End date.
Day count basis: 0 US (NASD) 30/360; 1 Actual/actual; 2 Actual/360; 3 Actual/365; 4 European 30/360.
More options: rounding and decimals
Precision and rounding
Rounding mode applied once to each result. Default half_even.
Round results to this many digits after the decimal point.
Round results to this many significant digits.

Examples

Example: Microsoft's example: 2012, US 30/360

Yearfrac
Fraction of a year.
0.58055556 rounded

Rounding: Half even, 8 decimal places. Calculator excel/yearfrac@1.

How it is calculated

days (by basis) ÷ year length (by basis)

YEARFRAC(start_date, end_date, [basis]). Basis 1 follows Excel exactly: within a year it divides by 365 or 366 (366 when the span includes 29 February); over longer spans it divides by the average length of the calendar years touched. Basis 0 treats the last day of February as the 30th. The order of the dates does not matter.

  • Arguments are named and ordered as in Excel. Excel computes in binary floating point; this API computes the exact value of the same formula and rounds it once, so it agrees with Excel to the digits Excel can show, and where Excel's own rounding error reaches the 15th digit this answer is the correct one.
  • Dates are ISO 8601 (YYYY-MM-DD) from 1900-03-01 to 9999-12-31, so Excel's fictional 29 February 1900 never comes into play. Each date result also gives Excel's serial number (1900 date system).

References

Results follow each calculator's documented rules and the sources listed. They are not financial, tax or legal advice. Check that the rules fit your case before you rely on a result. See the terms.

Use it from your code

POST /v1/calc/excel/yearfracVersion 1
curl https://api.curatedapi.com/v1/calc/excel/yearfrac \
  -H "Authorization: Bearer $CURATEDAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "start_date": "2012-01-01",
    "end_date": "2012-07-30",
    "options": {"decimals": 8}
  }'
The same call in Python
import os

import requests

payload = {
    "start_date": "2012-01-01",
    "end_date": "2012-07-30",
    "options": {
        "decimals": 8,
    },
}
response = requests.post(
    "https://api.curatedapi.com/v1/calc/excel/yearfrac",
    headers={"Authorization": f"Bearer {os.environ['CURATEDAPI_KEY']}"},
    json=payload,
    timeout=10,
)
print(response.json()["result"])
The same call in JavaScript
const payload = {
    "start_date": "2012-01-01",
    "end_date": "2012-07-30",
    "options": {
        "decimals": 8,
    },
};
const response = await fetch("https://api.curatedapi.com/v1/calc/excel/yearfrac", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.CURATEDAPI_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(payload),
});
console.log((await response.json()).result);

MCP tool calc_excel_yearfrac at https://mcp.curatedapi.com. Inputs, outputs and errors in the API reference.

Put it on your website

<div data-curatedapi-widget="excel/yearfrac" data-key="capi_public_…"></div>
<script src="https://widgets.curatedapi.com/v1/widget.js" async></script>

Use a public key registered for your site. Calculations are billed like API calls. How widgets work.

Related calculators

Get Excel's answer in your own code

Moving a spreadsheet model into an app? The API returns what Excel returns for the same inputs, so your users see the same numbers in both.

Free for up to 1,000 calculations a month. 50,000 calculations a month cost $9.00 on the Starter plan.