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

1 回表示 (過去 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 Center および File ExchangeHolidays / Seasons についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by