Why between(datetime1, datetime2) is different from (-1)*between(datetime2, datetime1)?

3 visualizaciones (últimos 30 días)
I am trying to measure the number of 'quarters' apart, between a single datetime r, and a vector of datetimes v. Could somebody please explain why Q1 & Q2 are different?
r = datetime(1991,6,30,'Format','dd/MM/yyyy');
v = dateshift(datetime(1990,12,31,'Format','dd/MM/yyyy'),'end','quarter',0:5);
%CASE 1
dif = between(r, v, 'quarters')
dif = 1×6 calendarDuration array
-1q 0q 0q 1q 2q 3q
Q1 = split(dif, 'quarters')
Q1 = 1×6
-1 0 0 1 2 3
%CASE 2
dif = between(v, r, 'quarters')
dif = 1×6 calendarDuration array
2q 1q 0q -1q -2q -3q
Q2 = (-1)*split(dif, 'quarters')
Q2 = 1×6
-2 -1 0 1 2 3
  1 comentario
the cyclist
the cyclist el 4 de Mzo. de 2024
Editada: the cyclist el 4 de Mzo. de 2024
A distilled version of the question is, why is there a "magnitude" difference between the outputs below? It is admittedly counter-intuitive.
dt1 = datetime("30/06/1991","Format",'dd/MM/yyyy');
dt2 = datetime("31/12/1990","Format",'dd/MM/yyyy');
between(dt1,dt2)
ans = calendarDuration
-5mo -30d
between(dt2,dt1)
ans = calendarDuration
6mo

Iniciar sesión para comentar.

Respuestas (1)

Voss
Voss el 4 de Mzo. de 2024
Movida: Dyuman Joshi el 5 de Mzo. de 2024
From the between documentation:
"In general, t2 is not equal to t1 + dt, unless you include 'time' in components."
  3 comentarios
Dyuman Joshi
Dyuman Joshi el 5 de Mzo. de 2024
r = datetime(1991,6,30,'Format','dd/MM/yyyy')
r = datetime
30/06/1991
v = dateshift(datetime(1990,12,31,'Format','dd/MM/yyyy'),'end','quarter',0:5)
v = 1×6 datetime array
31/12/1990 31/03/1991 30/06/1991 30/09/1991 31/12/1991 31/03/1992
The difference is not symmetric/identical for every time component -
str = {'years','quarters','months','weeks','days' };
for k=1:numel(str)
disp(str{k})
disp('CASE 1')
disp(between(r, v, str{k}))
disp('CASE 2')
disp(between(v, r, str{k}))
disp(' ')
end
years
CASE 1
0d 0d 0d 0d 0d 0d
CASE 2
0d 0d 0d 0d 0d 0d
quarters
CASE 1
-1q 0q 0q 1q 2q 3q
CASE 2
2q 1q 0q -1q -2q -3q
months
CASE 1
-5mo -2mo 0mo 3mo 6mo 9mo
CASE 2
6mo 3mo 0mo -3mo -6mo -9mo
weeks
CASE 1
-25w -13w 0w 13w 26w 39w
CASE 2
25w 13w 0w -13w -26w -39w
days
CASE 1
-181d -91d 0d 92d 184d 275d
CASE 2
181d 91d 0d -92d -184d -275d
Haris K.
Haris K. el 5 de Mzo. de 2024
Thank you. The comment in the documentation is clearly understood. What I do not understand is why the order of passing inputs t1 and t2, should alter the result.

Iniciar sesión para comentar.

Categorías

Más información sobre Holidays / Seasons en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by