File Exchange

image thumbnail


version (2.77 KB) by Michelle Hirsch
String representation of time without date in MATLAB

1 Download

Updated 04 Dec 2019

From GitHub

View Version History

View license on GitHub

Convert serial date numbers into time strings. Time strings are of HH:MM:SS.SSS format - like date strings, but without the date.
Note: A time datetime object was introduced in MATLAB R2014b which is way better than this little utility. If you've got access to it, use it instead!

Cite As

Michelle Hirsch (2021). timestr (, GitHub. Retrieved .

Comments and Ratings (4)

Stephen Cobeldick

A faster version, which correctly handles rounding and zero decimal digits:

function str = timestr(dtn,prc)
% Return a string representation of a serial date number: 'HH:MM:SS.SSSS'.
% (c) 2014 Stephen Cobeldick
% Syntax:
% str = timestr
% str = timestr(dtn)
% str = timestr(dtn,prc)
% Convert the time portion of a serial date number into a string, formatted
% as 'HH:MM:SS.SSSS', with optional control over the seconds' precision.
% Input <dtn> can be a scalar, or an array of any dimensions: the output
% <str> is a char array whose rows are linear indexed from <dtn>.
% This is a drop-in replacement for Michelle Hirsch's "timestr": it fixes
% some rounding issues, allows zero decimal digits, and is faster!
% ### Examples ###
% Examples use the date+time described by the vector [1999,1,3,15,6,48.0568].
% timestr
% ans = '15:06:48.0568'
% timestr(730123.62972287962)
% ans = '15:06:48.0568'
% timestr(730123.62972287962,2)
% ans = '15:06:48.05'
% timestr(730123.62972287962,0)
% ans = '15:06:48'
% timestr([730123.62972287962,0.62972287962],3)
% ans = ['15:06:48.056';'15:06:48.056']
% ### Input and Output Arguments ###
% Inputs (*=default):
% dtn = NumericArray, the SerialDateNumbers to convert, any size not empty, *now.
% prc = NumericScalar, the number of decimal digits of the seconds, *4.
% Output:
% str = CharArray, with numel(dtn) rows. Each row is linear indexed from <dtn>.
% str = timestr(*dtn,*prc)

if nargin<1
dtn = now;
assert(isnumeric(dtn)&&isreal(dtn),'First input must be a real numeric.')
if nargin<2
prc = 4;
assert(isnumeric(prc)&&isscalar(prc),'Second input must be a scalar numeric.')
[~,~,~,H,M,S] = datevecmx(dtn(:).');
fmt = sprintf('%%02d:%%02d:%%0%d.%df',prc+2+(prc>0),prc);
str = reshape(sprintf(fmt,[H;M;S-rem(S,10^-prc)]).',[],numel(dtn)).';

Stephen Cobeldick

Minor bug fix for my suggested code below: the logical compare 'prc>0' needs to be in brackets '(prc>0)'.

Stephen Cobeldick

A useful concept but there are multiple mlint messages that need attention, and the author themselves makes one comment that things are "a bit messed up": no array preallocation, no logical indexing, unused variables, for-loops instead of vectorized code, vague input/output specifications, many commented-out lines of code... keep an eye on the ball, MATLAB!

Apart from tidying up the code, some design issues allow edge-case bugs to creep in too: the seconds' fractional part is rounded inside an "sprintf" call, which can result in some unexpected time values in the string:
-> ans = '23:59:60.00'
-> ans = ' 23:59:060'

The following code fixes this behavior by truncating at the required precision (consistent with how we increment time units only after they are complete), as well as being faster to compute:

%%% begin mfile code %%%
function str = timestrX(num,prc)
% Return a string representation of a serial date number: 'HH:MM:SS.SSSS'.
% num = NumericArray, the SerialDateNumbers to convert, any size not empty.
% prc = NumericScalar, the number of decimal digits of the seconds, default=4.
% str = CharArray, with numel(num) rows. Each row is linear indexed from num.
if nargin<2, prc = 4; end
[~,~,~,H,M,S] = datevecmx(num(:).');
fmt = sprintf('%%02d:%%02d:%%0%d.%df',prc+2+prc>0,prc);
str = reshape(sprintf(fmt,[H;M;S-rem(S,10^-prc)]).',[],numel(num)).';
%%% end mfile code %%%

-> ans = '23:59:59.99'
-> ans = ' 23:59:59'

Shi Shu

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!