how to calculate common dates from two date arrays?
66 views (last 30 days)
Show older comments
Sven Larsen
on 17 Oct 2025 at 15:23
Commented: dpb
on 19 Oct 2025 at 13:08
I have two arrays with many datenums in format [startTime endTime]. I am trying to find a way to calculate all common datetimes (i.e duration), and end product should be new array where is all [startTime endTime] of all common datetimes.
Example (datenums are datetime for clarity in this example): dates1 array some row is :
[1.10.2025 10:00:00, 1.10.2025 13:00:00]
and some rows in dates2 are:
[1.10.2025 09:30:00, 1.10.2025 11:00:00]
[1.10.2025 12:30:00 ,1.10.2025 14:40:00]
so new array of common times will get rows
[1.10.2025 10:00:00, 1.10.2025 11:00:00]
[1.10.2025 12:30:00, 1.10.2025 13:00:00]
Tried to ask Grok but its function was erroneus. Greatly appreciate help!
5 Comments
Dyuman Joshi
on 17 Oct 2025 at 16:57
"they are all datenums and for example unique([dates1.dStrt]); gives 339 unique values...."
Yes, it escaped me that they can be datenums().
It seems I am missing something -
load('dates.mat')
whos
dates1
y = dates1.dStrt;
size(y)
z = vertcat(dates1.dStrt);
size(z)
"same algorithm works finding common number ranges also :)"
Similar logic will work. Same algorithm might not.
Please check Star Strider's answer below.
dpb
on 18 Oct 2025 at 12:21
Accepted Answer
dpb
on 18 Oct 2025 at 16:36
Edited: dpb
on 19 Oct 2025 at 13:05
load dates
clear ans
dates1=[datetime(vertcat(dates1.dStrt),'convertfrom','datenum') datetime(vertcat(dates1.dStp),'convertfrom','datenum')];
tD1=array2table(dates1,'VariableNames',{'Start','Stop'});
tD1.Length=tD1.Stop-tD1.Start;
head(tD1)
Ooops!!! The start/stop times appear to be reversed as the length of each time span is negative.,,,we'll just recreate the table as expected.
tD1=array2table(dates1,'VariableNames',{'Stop','Start'});
tD1.Length=tD1.Stop-tD1.Start;
head(tD1)
dates2=[datetime(vertcat(dates2.dStrt),'convertfrom','datenum') datetime(vertcat(dates2.dStp),'convertfrom','datenum')];
tD2=array2table(dates2,'VariableNames',{'Stop','Start'});
tD2.Length=tD2.Stop-tD2.Start;
head(tD2)
Now it's not quite clear to me what you actually want as a result -- for each start time in tD1 the records that are in the duration of that time span whose start time is within that length of time or which overlaps or ...?
Can finish up once have a clear definition.
ixoverlap=arrayfun(@(t_s,t_e)(t_s<=tD2.Stop)&(t_e>=tD2.Start),tD1.Start,tD1.Stop,'uni',0);
ix=ixoverlap{1};
for i=2:numel(ixoverlap)
ix=ix|ixoverlap{i};
end
res=tD1(ix,:);
height(res)
res
I THINK the above will find those that are overlapping; check the results to see if are what you were looking for.
1 Comment
dpb
on 19 Oct 2025 at 13:08
ERRATUM/ADDENDUM
NOTA BENE: I forgot to remove the 1:10 debugging short subset subscripts on the arguments in the arrayfun() line above; I just made the edit to remove but the result will be many more than 10, I'm certain.
More Answers (0)
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!