need to find time difference between two time strings
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
H D
el 16 de Dic. de 2014
Respondida: David Young
el 16 de Dic. de 2014
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 comentarios
David Young
el 16 de Dic. de 2014
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?
Respuesta aceptada
Guillaume
el 16 de Dic. de 2014
datetime(d2, 'ConvertFrom', 'datenum') - datetime(d1, 'ConvertFrom', 'datenum')
Más respuestas (1)
David Young
el 16 de Dic. de 2014
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.
0 comentarios
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!