how to calculate common dates from two date arrays?
Show older comments
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
load('dates.mat')
whos
dates1
y = dates1.dStrt;
class(y)
y(1)
unique(y)
z= dates1.dStp;
class(z)
z(1)
unique(z)
Note - The dates1 data you have attached contains numeric double values (with all the values being same), not datenums - as you can see above.
I'm not sure how to interpret this.
Sven Larsen
on 17 Oct 2025
Edited: Sven Larsen
on 17 Oct 2025
datenum(now)
This is tthe same range as the 7.3933e+05 shown above, so the values are indeed datenum. The values are not datetime objects.
"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
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time 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!