How to get difference between two time values ?

12 ビュー (過去 30 日間)
Haritha
Haritha 2019 年 5 月 10 日
回答済み: Peter Perkins 2019 年 5 月 14 日
How we will get difference between the below times. I tried with caldiff, diff, substraction commands its not working . Please help me if any one knows
Capture2.JPG

採用された回答

Dennis
Dennis 2019 年 5 月 10 日
You can use etime, but you need to convert your strings first. I hope this helps:
a=datetime
pause(5)
b=datetime
a=datevec(a);
b=datevec(b);
etime(b,a)

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2019 年 5 月 14 日
Don't use etime. Use datetimes. Haritha, without more info it's hard to tell, but it looks like your "data" is a cellarray of char vectors. You have to start with the right kind of data. Do this:
>> c
c =
4×1 cell array
{'17-Apr-2019 11:40:10'}
{'17-Apr-2019 11:40:12'}
{'23-Apr-2019 16:11:41'}
{'23-Apr-2019 16:11:47'}
>> t = datetime(c)
t =
4×1 datetime array
17-Apr-2019 11:40:10
17-Apr-2019 11:40:12
23-Apr-2019 16:11:41
23-Apr-2019 16:11:47
>> caldiff(t) % calendar unit diffs
ans =
3×1 calendarDuration array
0h 0m 2s
6d 4h 31m 29s
0h 0m 6s
>> diff(t) % exact time times
ans =
3×1 duration array
00:00:02
148:31:29
00:00:06
>> t - t(1) % offset from first time
ans =
4×1 duration array
00:00:00
00:00:02
148:31:31
148:31:37

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by