How to get difference between two time values ?

7 Ansichten (letzte 30 Tage)
Haritha
Haritha am 10 Mai 2019
Beantwortet: Peter Perkins am 14 Mai 2019
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

Akzeptierte Antwort

Dennis
Dennis am 10 Mai 2019
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)

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 14 Mai 2019
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

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by