필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Haris K.
Haris K. 2024년 3월 4일
댓글: Haris K. 2024년 3월 5일
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
the cyclist
the cyclist 2024년 3월 4일
편집: the cyclist 2024년 3월 4일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2024년 3월 4일
이동: Dyuman Joshi 2024년 3월 5일
From the between documentation:
"In general, t2 is not equal to t1 + dt, unless you include 'time' in components."
  댓글 수: 3
Dyuman Joshi
Dyuman Joshi 2024년 3월 5일
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. 2024년 3월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Chemistry에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by