need to find time difference between two time strings

Hi I have two (as a sample) matlab serial date numbers which I want to find the gap between them. hours minutes and seconds. these are the time I have: d1 = 7.314983860995370e+005 d2 = 7.314983864467592e+005
any help is greatly appreciated

댓글 수: 2

These are not strings. Are they MATLAB serial date numbers (i.e. do they both refer to 9 October 2002), or are they something else?
H D
H D 2014년 12월 16일
편집: H D 2014년 12월 16일
thanks for your time. Yes you are right. serial date number is what they are, and the time is as you said. thanks for correction. I fixed the question also.

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2014년 12월 16일
datetime(d2, 'ConvertFrom', 'datenum') - datetime(d1, 'ConvertFrom', 'datenum')

댓글 수: 3

H D
H D 2014년 12월 16일
thanks for your response. Unfortunately I am not able to use datetime as it is not defined in the version of matlam I am using. is there any other suggestion? or any help to use datetime in the older version of matlab (plug and play solution etc.)
Guillaume
Guillaume 2014년 12월 16일
편집: Guillaume 2014년 12월 16일
You can also directly subtract datenums and convert them into date strings:
datestr(d2-d1, 'HH:MM:SS')
This works pre-2014b
H D
H D 2014년 12월 16일
thanks in advance. It is working perfectly as you mentioned. I appreciate it

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Young
David Young 2014년 12월 16일
Something like this:
% Some data. Making the gap between d1 and d2 bigger than in your example
% to provide a better check on results
d1 = 7.314983860995370e+005; % 09-Oct-2002 09:15:59
d2 = now;
% Difference in days
daydiff = d2 - d1;
% Subdivisions of days into hours, hours into mins, mins into secs
divisions = [1 24 60 60];
% Get number of days, hrs, mins and secs between times, rounded down
dhmsTotal = floor(daydiff * cumprod(divisions));
% Subtract days*24 from total hrs, etc., to get remainders
dhms = dhmsTotal - [0 dhmsTotal(1:end-1)] .* divisions;
dhms is now a vector with [days hours mins secs]. Note that the seconds are rounded down to whole seconds - if you want fractions of seconds, apply the floor function only to the first three elements of dhmsTotal.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

H D
2014년 12월 16일

답변:

2014년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by