To add two datetime arrays , with millisecond values

37 ビュー (過去 30 日間)
Srey
Srey 2021 年 11 月 7 日
コメント済み: Srey 2021 年 11 月 8 日
I have a datetime array a=[ 09:39:12.000, 09:39:12.000] and another datetime array with b= [ 0:00:00.001, 0:00:00.002]
I want answer as [09:39:12.001, 09:39:12.002]. FIrst value of a added to first value of b, and so on.
It is showing 'Addition is not defined between datetime arrays'. Is there any way to add the hh:mm:ss of first array to .SSS of second array
Is there any way to add the 2 datetime arrays? Please help.

採用された回答

Stephen23
Stephen23 2021 年 11 月 7 日
編集済み: Stephen23 2021 年 11 月 7 日
You should be storing those times as duration objects (not as datetime objects) so you could simply add them. The solution to your task is to convert them to duration objects, which can be simply added together:
a = datetime(0,0,0,[9;9],[39;39],[12;12],'Format','HH:mm:ss.SSS') % better as DURATION
a = 2×1 datetime array
09:39:12.000 09:39:12.000
b = datetime(0,0,0,0,0,[0.001;0.002],'Format','HH:mm:ss.SSS') % better as DURATION
b = 2×1 datetime array
00:00:00.001 00:00:00.002
c = timeofday(a)+timeofday(b);
c.Format = 'hh:mm:ss.SSS'
c = 2×1 duration array
09:39:12.001 09:39:12.002
  1 件のコメント
Srey
Srey 2021 年 11 月 8 日
Thats a lot, It worked!!

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 11 月 7 日
You cannot add two datetime arrays as the error message indicates. If addition of datetime arrays was defined, what would be the value of (tomorrow + next Tuesday)?
You can add two duration arrays (as Stephen shows) or a duration and a datetime.
x = minutes(45);
y = minutes(30);
n = datetime('now')
n = datetime
07-Nov-2021 21:34:58
z = x+y % 75 minutes
z = duration
75 min
w = n + y % half an hour from now
w = datetime
07-Nov-2021 22:04:58

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by