yearfrac
Fraction of year between dates
Description
returns a fraction, in years, based on the number of days between dates
YearFraction
= yearfrac(StartDate
,EndDate
,Basis
)StartDate
and EndDate
using the given
day-count Basis
.
The number of days in a year (365 or 366) is equal to the number of days in the
calendar year after the StartDate
. If
EndDate
is earlier than StartDate
,
YearFraction
is negative.
All specified arguments must be M
-by-NUMINST
conforming matrices or scalar arguments.
Examples
Compute yearfrac
When the Calendar Year After the StartDate is Not a Leap Year
Given a Basis
of 0
and a Basis
of 1
, compute yearfrac
.
Define the StartDate
and EndDate
using a Basis
of 0
.
YearFraction = yearfrac('14 mar 01', '14 sep 01', 0)
YearFraction = 0.5041
Define the StartDate
and EndDate
using a Basis
of 1
.
YearFraction = yearfrac('14 mar 01', '14 sep 01', 1)
YearFraction = 0.5000
Compute yearfrac
When the Calendar Year After the StartDate is a Leap Year
Given a Basis
of 0
, compute yearfrac
when the calendar after StartDate
is in a leap year.
Define the StartDate
and EndDate
using a Basis
of 0
.
yearFraction = yearfrac(' 14 mar 03', '14 sep 03', 0)
yearFraction = 0.5027
There are 184 days between March 14 and September 14, and the calendar year after the StartDate
is a leap year, so yearfrac
returns 184/366 = 0.5027.
Compute the Fraction of a Year Using an actual/actual Basis
To get the fraction of a year between '31-Jul-2015' and '30-Sep-2015' using the actual/actual basis:
yearfrac('31-Jul-2015', '30-Sep-2015', 0)*2
ans = 0.3333
For the actual/actual basis, the fraction of a year is calculated as:
(Actual Days between Start Date and End Date)/(Actual Days between Start Date and exactly one year after Start Date)
There are 61 days between 31-Jul-2015 and 30-Sep-2015. Since the next year is a leap year, there are 366 days between 31-Jul-2015 and 31-Jul-2016. So, there is 61/366 which is exactly 1/6. So given this, exactly 2/6 is the expected result for the fraction of the six-month period.
Compute yearfrac
When the Date Range Spans a Leap Year
Use a Basis
of 12 to compute yearfrac
when parts of the date range are in leap year and in a non-leap year. The output YearFraction
is a fraction, in years, based on the number of days between the StartDate
and EndDate
.
YearFraction = yearfrac('1-Jan-2016','30-Jan-2017',12)
YearFraction = 1.0795
Compute yearfrac
When Specifying datetimes
Given a Basis
of 9
, compute yearfrac
when the StartDate
and EndDate
are specified using datetimes.
yearfrac(datetime(2000,1,1), datetime(2001,1,1), 9)
ans = 1.0167
Input Arguments
StartDate
— Start date
datetime array | string array | date character vector
Start date, specified as a scalar or an M
-by-N
vector
using a datetime array, string array, or date character vectors.
To support existing code, yearfrac
also
accepts serial date numbers as inputs, but they are not recommended.
Data Types: char
| string
| datetime
EndDate
— End date
datetime array | string array | date character vector
End date, specified as a scalar or an M
-by-N
vector
using a datetime array, string array, or date character vectors.
To support existing code, yearfrac
also
accepts serial date numbers as inputs, but they are not recommended.
Data Types: char
| string
| datetime
Basis
— Day-count basis for each set of dates
0
(actual/actual) (default) | vector of numerics with values 0
through 13
Day-count basis for each set of dates, specified as a scalar or an
M
-by-N
matrix of integers with
values of 0
through 13
.
0 = actual/actual (default)
1 = 30/360 (SIA)
2 = actual/360
3 = actual/365
4 = 30/360 (BMA)
5 = 30/360 (ISDA)
6 = 30/360 (European)
7 = actual/365 (Japanese)
8 = actual/actual (ICMA)
9 = actual/360 (ICMA)
10 = actual/365 (ICMA)
11 = 30/360E (ICMA)
12 = actual/365 (ISDA)
13 = BUS/252
For more information, see Basis.
Data Types: single
| double
Output Arguments
YearFraction
— Real numbers identifying interval, in years, between StartDate
and EndDate
vector
Real numbers identifying the interval, in years, between StartDate
and
EndDate
, returned as a scalar or an
M
-by-N
matrix.
More About
Difference Between yearfrac
and date2time
The difference between yearfrac
and date2time
is that date2time
counts
full periods as a whole integer, even if the number of actual days
in the periods are different. yearfrac
does not
count full periods.
For example,
yearfrac('1/1/2000', '1/1/2001', 9) ans = 1.0167
yearfrac
for Basis
9
(ACT/360
ICMA) calculates 366/360 = 1.0167. So, even if the dates have the
same month and date, with a difference of 1 in the year, the returned
value may not be exactly 1. On the other hand, date2time
calculates one full year period:
date2time('1/1/2000', '1/1/2001', 1, 9) ans = 1
Version History
Introduced before R2006aR2022b: Serial date numbers not recommended
Although yearfrac
supports serial date numbers,
datetime
values are recommended instead. The
datetime
data type provides flexible date and time
formats, storage out to nanosecond precision, and properties to account for time
zones and daylight saving time.
To convert serial date numbers or text to datetime
values, use the datetime
function. For example:
t = datetime(738427.656845093,"ConvertFrom","datenum"); y = year(t)
y = 2021
There are no plans to remove support for serial date number inputs.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)