How to get difference between two time values ?

12 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Dennis
Dennis il 10 Mag 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)

Più risposte (1)

Peter Perkins
Peter Perkins il 14 Mag 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

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by