Time difference in 'HH:mm:ss,SSSSSS' format

19 views (last 30 days)
Hi
I have several absolute times in form of 'HH:mm:ss,SSSSSS' in a CSV (seperator is ; ).
What I need is the expired time in seconds, relative to the start time t0.
So I tried several ways:
1) Using datevec and etime
t0 = '15:30:25,123456'
t1 = '16:45:30,123457'
t0_vec = datevec(t0,'HH:mm:ss,SSSSSS') % Works only in 'HH:mm:ss'
t1_vec = datevec(t1,'HH:mm:ss,SSSSSS') % Works only in 'HH:mm:ss'
dt = etime(t1,t0);
This got me:
Error using matlab.internal.datetime.cnv2icudf (line 162)
Unrecognized second format. Format: HH:mm:ss,SSSSSS.
2) Using datetime and "-" operator
t0 = '15:30:25,123456'
t1 = '16:45:30,123457'
t0_datetime = datetime(t1,'InputFormat','HH:mm:ss,SSSSSS','Format','HH:mm:ss,SSSSSS')
t1_datetime = datetime(t1,'InputFormat','HH:mm:ss,SSSSSS','Format','HH:mm:ss,SSSSSS')
dt = t1-t0
This got me:
ans =
duration
05:32:08
(no fractional seconds.. also not when i exportet it )
Thanks for your help!

Accepted Answer

Steven Lord
Steven Lord on 23 Feb 2021
t0 = '15:30:25,123456'
t0 = '15:30:25,123456'
t1 = '16:45:30,123457'
t1 = '16:45:30,123457'
theFormat = 'hh:mm:ss.SSSSSS';
timeStart = duration(replace(t0, ',', '.'), 'InputFormat', theFormat, 'Format', theFormat)
timeStart = duration
15:30:25.123456
timeEnd = duration(replace(t1, ',', '.'), 'InputFormat', theFormat, 'Format', theFormat)
timeEnd = duration
16:45:30.123457
elapsed = timeEnd - timeStart
elapsed = duration
01:15:05.000001

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!