MATLAB: Duration & Calendar Duration. What is the difference

dates and durationsfundamentalsMATLAB

Duration: doc duration
Calendar Duration: doc calendarDuration
Still, I can't understand the difference.
Piece of code which exemplifies my doubt:
I added exactly 1 year to t0, using years and calyears, and I get different answers.

Best Answer

Hello vc,
Calyears and years are two different quantities. Calyears add a year, taking into account leap years, and gives the same date and time one year later.
>> t17 = datetime(2017,3,1,0,0,0) t17 = 01-Mar-2017 00:00:00
>> t17 + calyears(1) ans = 01-Mar-2018 00:00:00
>> t19 = datetime(2019,3,1,0,0,0) t19 = 01-Mar-2019 00:00:00
>> t19 + calyears(1) ans = 01-Mar-2020 00:00:00
On the other hand, years adds a fixed interval representing an average length year of 365.2425 days, which is 365 days, 5 hours, 49 min and 12 sec.
>> t17 + years(1) ans = 01-Mar-2018 05:49:12
>> t19 + years(1) ans = 29-Feb-2020 05:49:12
2018 is not a leap year, so in the first case you end up advanced 5 hr 49 min 12 sec into March 1st 2018. But 2020 is a leap year, so in the second case you are 18 hr 10 min 48 sec short of making it into March 1st 2020.
For some reason Matlab uses 365.2425 days whereas most of the sources on the web cite something like 365.24217.