Duration & Calendar Duration. What is the difference?

13 visualizzazioni (ultimi 30 giorni)
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.
  1 Commento
Stephen23
Stephen23 il 16 Feb 2017
Modificato: Stephen23 il 16 Feb 2017
From the duration documentation: "..fixed-length (365.2425-day long) years."
From the calendar duration documentation: "..elapsed time in calendar units of variable length. For example, the number of days in 1 month depend on the particular month of the year. Calculations with calendar durations account for daylight saving time changes and leap years"
So duration has a fixed year length, and calendar duration the year length changes with leap years, etc.

Accedi per commentare.

Risposta accettata

David Goodmanson
David Goodmanson il 16 Feb 2017
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.

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by