How to get difference between two time values ?

12 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by