File Exchange

image thumbnail

Convert seconds to human readable string(s)

version 1.6 (3.39 KB) by Rody Oldenhuis
Convert an amount of seconds to hours/days/weeks/etc.


Updated 02 May 2020

From GitHub

View Version History

View license on GitHub

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function converts a (usually large) amount of seconds to human-readable string. For example,
>> str = seconds2human(1463456.3)
str =
'About 2 weeks and 2 days.'
You may also call the function with a second input argument; either 'short' (the default) or 'full'. This determines the level of detail returned in the string:
>> str = seconds2human(1463456.3, 'full')
str =
'2 weeks, 2 days, 22 hours, 30 minutes, 56 seconds.'

The 'short' format returns only the two largest units of time.

Furthermore, [secs] may be an NxM-matrix, in which case the output is an NxM cell array of the corresponding strings.

Cite As

Rody Oldenhuis (2021). Convert seconds to human readable string(s) (, GitHub. Retrieved .

Comments and Ratings (15)

Pierluigi Martelli

Rody Oldenhuis

@Omri Bar-Elli, thanks for your kind words. I just fixed the warning; should not happen anymore.

Omri Bar-Elli

Great function. Thank you!
On Matlab R2016b it gives a warning:
Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead.
> In seconds2human (line 46)

Santiago Echeverri

Thank you, works nicely.

Naor Movshovitz

J.R.! Menzinger

Esteban Echeverria

Amazing. I had to change the format a little to get my answer in time format. 00:00:00. Still great :) Very good for waitbars.

Husam Aldahiyat

Love it. Only complaint is the name is too long (had to rename it).

Jesse Blocher

Works quite well - only critique is that it doesn't handle <0.5 seconds very well. It just returns an empty string. Perhaps is could default to "< 1 second" for anything less than that.


Worked perfectly for me. Thanks.

good... I was nice :)

Rody Oldenhuis

yup, they are, depending though on your definition of "scientific". They are not SI units (the only unit of time there is the second), but based on properly defined quantities (second, year) and thus derived units, used in many standards such as ISO-8601 and JD in astronomy.

Anyway, thanks fer the comments, much appreciated ^_^ I didn't know ETIME yet, thanks for that too.

Jos (10584)

The code has indeed improved and comments have been added, as well as a readable help section (missing, however, a reference to, e.g., DATENUM and ETIME, which also may be used to get the elapsed time in a more readable format).

Still, I doubt it's usefulness and emphasis on details when unneeded. I think 3 stars is a nice rating for this snippet.

btw, are weeks, months, or even days proper scientific units?

Rody Oldenhuis

woops! I actually did not check this -ancient- piece of code before I uploaded it. My bad, this program came from a time when I was learning the basics of matlab. Anyway, should be lots better now.

Coincidentally, "Eon" is not a scientific unit of time, so I did not include it ^_^

Jos (10584)

You missed the eons!

More importantly, you should provide a proper help section, giving details of the input and expected output.The code is also not that nice. Internal comments are almost lacking. MatLab already offers DEBLANK and STRTRIM for trimming white spaces. Moreover, this function is not needed if you had used cell arrays to store the names. The section of "switch plural(amount), case true, ... case false ..., end is much simpler codes as: if amount>1, ... else ... end.

Finally, is it really useful to know the number of seconds remaining when something took millenia, or even days? This focus on unimportant details reminds me of an old russian joke: A man has saved enough money to buy a car in the old Sovjet-Union. The car-dealer tells him that he can pick up his new car two years from now. The man asks: "In the morning or in the afternoon? ". "Why do you care? It is 2 years from now" the car-dealer replies. "Well", the man says, "the plumber is coming in the morning".

MATLAB Release Compatibility
Created with R2009b
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!