MATLAB Answers

Converting hh:mm:ss into seconds

488 views (last 30 days)
tethys
tethys on 20 Jan 2012
Answered: Peter Seibold on 18 Apr 2021 at 13:19
Hi all,
I want to read the vectors which are hh:mm:ss (14:35:59.812) format and convert it to seconds. How can I do this in matlab?
Best

Accepted Answer

Andreas Goser
Andreas Goser on 20 Jan 2012
Is this what you are looking for?
t='14:35:59.812';
[Y, M, D, H, MN, S] = datevec(t);
H*3600+MN*60+S
  3 Comments
per isakson
per isakson on 3 Dec 2016
On R2016a I get the expected result
>> dt = datestr( now,'HH:MM:SS,FFF')
dt =
12:58:26,797

Sign in to comment.

More Answers (2)

per isakson
per isakson on 20 Jan 2012
The first and the last cell returns the result I think you are looking for. See help on DATENUM
datenum( '14:35:59.812', 'HH:MM:SS.FFF' ) .* (24*60*60) - ...
datenum( '00:00:00.000', 'HH:MM:SS.FFF' ) .* (24*60*60)
datestr( datenum( '00:00:00.000', 'HH:MM:SS.FFF' ), 'yyyy-mm-dd' )
datenum( '14:35:59.812', 'HH:MM:SS.FFF', 0 ) .* (24*60*60)
  1 Comment
tethys
tethys on 20 Jan 2012
Thansk for the answer, it is very useful information:)

Sign in to comment.


Peter Seibold
Peter Seibold on 18 Apr 2021 at 13:19
About 100 times faster is:
t='14:35:59.812';
seconds=sum(sscanf(t,'%f:%f:%f').*[3600;60;1]);

Community Treasure Hunt

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

Start Hunting!