A better YEARFRAC()

chrisw76

New Member
Joined
Sep 23, 2016
Messages
7
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello!

Recently I was using the YEARFRAC() function again for the first time in a while and again noticed that it doesn't quite work as expected with the actual / actual basis (majority of my use cases). I'm not sure of the underlying logic to it, but it just doesn't seem to handle leap years very well or it is doing it to a much higher level of precision than required. That is, is may be taking into account the century / 400 not being a leap year. Whatever the case I'd expect that a formula like:

Excel Formula:
=YEARFRAC( DATE(2020, 1, 1),  YEARFRAC( DATE(2023, 12, 31) ) + 1, 1)

I'd expect it to return 4 but it returns 3.998357964 instead.

So, this is the formula I came up with to give me what I want. What it does calculate the beginning year fraction, the ending year fraction and any whole years in the middle and add them together. Here it is:

Excel Formula:
=LET(
    _sd, MIN(<cell_1>, <cell_2>),
    _ed, MAX(<cell_1>, <cell_2>),
    _yr1Days, DATE(YEAR(_sd), 12, 31) - DATE(YEAR(_sd), 1, 1) + 1,
    _yr2Days, DATE(YEAR(_ed), 12, 31) - DATE(YEAR(_ed), 1, 1) + 1,
    _midYrs, YEAR(_ed) - YEAR(_sd) - 1,
    _yr1Frac, (DATE(YEAR(_sd), 12, 31) - _sd + 1) / _yr1Days,
    _yr2Frac, (_ed - DATE(YEAR(_ed), 1, 1) + 1) / _yr2Days,
    IF(V25 > V26, -1, 1) * (_yr1Frac + _midYrs + _yr2Frac)
)

It probably could be a bit less verbose, but it's my style :biggrin: . Are there any mistakes / optimisations I missed?

Cheers, Chris W.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
=YEARFRAC( DATE(2020, 1, 1), YEARFRAC( DATE(2023, 12, 31) ) + 1, 1)
Did you mean:

Excel Formula:
=YEARFRAC( DATE(2020, 1, 1),  DATE(2023, 12, 31) + 1, 1)
 
Upvote 0
And while I'm re-reading it the last line of my formula still had the cell references in it. It should be:

Excel Formula:
=LET(
    _sd, MIN(<cell_1>, <cell_2>),
    _ed, MAX(<cell_1>, <cell_2>),
    _yr1Days, DATE(YEAR(_sd), 12, 31) - DATE(YEAR(_sd), 1, 1) + 1,
    _yr2Days, DATE(YEAR(_ed), 12, 31) - DATE(YEAR(_ed), 1, 1) + 1,
    _midYrs, YEAR(_ed) - YEAR(_sd) - 1,
    _yr1Frac, (DATE(YEAR(_sd), 12, 31) - _sd + 1) / _yr1Days,
    _yr2Frac, (_ed - DATE(YEAR(_ed), 1, 1) + 1) / _yr2Days,
    IF(<cell_2> > <cell_1>, -1, 1) * (_yr1Frac + _midYrs + _yr2Frac)
)

I'll read my preview more carefully in future. :)
 
Upvote 0
That is, is may be taking into account the century / 400 not being a leap year
Why would 400 not be a leap year (other than when the Gregorian calendar started)?
The rules AFAIK for a leap year are...

The year must be evenly divisible by 4;

If the year can also be evenly divided by 100, it is not a leap year;
unless...
The year is also evenly divisible by 400. Then it is a leap year.

If it is divisible by 4000 it is not a leap year
 
Last edited:
Upvote 0
Why would 400 not be a leap year (other than when the Gregorian calendar started)?
The rules AFAIK for a leap year are...

The year must be evenly divisible by 4;

If the year can also be evenly divided by 100, it is not a leap year;
unless...
The year is also evenly divisible by 400. Then it is a leap year.

If it is divisible by 4000 it is not a leap year

Yes you're right: if a century year is dividable by 400 then it is a leap year otherwise it isn't. What I was getting at here is that I'd expect this function to return a 4 but it doesn't with one possible reason the way a year is calculated.

I haven't been able to determine exactly what the YEARFRAC() function is doing and wasn't able to find published logic on-line for how it actually calculates to explain it.

Cheers, Chris W.
 
Upvote 0
Someone online has claimed the below

"If one uses Excel's YEARFRAC(date1,date2,1) function then Excel will use the basis as per the second parameter. This does not matter if date1 and date2 are in the same year, but it does matter if date1 is a leap year (e.g. 2020) and date2 is a non-leap year (e.g. 2021)"

Still looking to see if I can see anything official
 
Upvote 0

Forum statistics

Threads
1,223,313
Messages
6,171,385
Members
452,397
Latest member
ddneptune

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top