Display elapsed time in hours, minutes, and seconds

Since now datestr is discouraged, I would like to use datetime to display elapsed time in hours, minuts and seconds. My previous code was
datestr(toc, 'HH:MM:SS')
How can I replace this with a command using datetime?

 Accepted Answer

time = seconds(4321)
time.Format = 'hh:mm:ss'

6 Comments

Thank you!
But is there also a one-liner? I have put my command within the composition of a message, and it would be nice to not break it into several pieces...
I tried
datetime(toc,'Format','HH:mm:ss')
or
datetime(seconds(toc),'Format','HH:mm:ss')
but both give errors.
Thank you!
But is there also a one-liner? I have put my command within the composition of a message, and it would be nice to not break oit into several pieces...
I tried
datetime(toc,'Format','yyyy-MM-dd HH:mm:ss.SSS')
or
datetime(seconds(toc),'Format','yyyy-MM-dd HH:mm:ss.SSS')
but that gives an error.
Oh Wow! That's perfect! Thank you!!
I just noticed that one can even print milliseconds using
time = duration(0,0,toc, 'Format', 'hh:mm:ss.SSSS')
the command seconds is returning an instance of the class duration, that's why you can include the 'Format' command in duration, but not in seconds.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!