Why between(datetime1, datetime2) is different from (-1)*between(datetime2, datetime1)?
Afficher commentaires plus anciens
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')
Q1 = split(dif, 'quarters')
%CASE 2
dif = between(v, r, 'quarters')
Q2 = (-1)*split(dif, 'quarters')
1 commentaire
the cyclist
le 4 Mar 2024
Modifié(e) : the cyclist
le 4 Mar 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)
between(dt2,dt1)
Réponses (1)
Voss
le 4 Mar 2024
Déplacé(e) : Dyuman Joshi
le 5 Mar 2024
1 vote
"In general, t2 is not equal to t1 + dt, unless you include 'time' in components."
3 commentaires
Dyuman Joshi
le 5 Mar 2024
Déplacé(e) : Dyuman Joshi
le 5 Mar 2024
dt1 = datetime("30/06/1991","Format",'dd/MM/yyyy');
dt2 = datetime("31/12/1990","Format",'dd/MM/yyyy');
between(dt1,dt2,'time')
between(dt2,dt1,'time')
r = datetime(1991,6,30,'Format','dd/MM/yyyy')
v = dateshift(datetime(1990,12,31,'Format','dd/MM/yyyy'),'end','quarter',0:5)
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
Haris K.
le 5 Mar 2024
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!