Convert seconds to [DD:][HH:]MM:SS

sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }
Sample Output
$ sec2dhms $SECONDS
$ sec2dhms 14101
$ sec2dhms 141
$ sec2dhms 1

2017-03-21 23:31:25

What Others Think

Mantaaaappp uy... Alhamdulillah,,,
Sevdiana · 240 weeks and 1 day ago
Wow absolutely wonderful. It leaves me just speechless and stunned too. It seems like just magical to do converting seconds with 14 characters. As system analyst from Nursing Essays Help taking hardly 15 minutes of practice I am now enable to write this command on my own.
velvetyoung · 79 weeks and 3 days ago
Get cheap nursing essay writing service for nursing students
stephenhawking · 79 weeks and 1 day ago
Good to know. Will share this at the in Guelph.
carmenjones · 70 weeks and 4 days ago
any new recent updates with the forum?? madison bathroom remodel
Killersmile · 42 weeks and 2 days ago
This would actually impress so many people, and all the attention would be diverted towards you. [url=]Harley Quinn Costume Jacket[/url] | [url=]Freddie Mercury Jacket[/url]
sarahjames999 · 23 weeks and 3 days ago
Convert seconds info is great. Regards: cheap flights
linaajulie121 · 18 weeks and 1 day ago
This is a very technical topic and I don't understand these types of posts but I am trying to make some interest in this field. Well, today check this site ?? Assignment Writing Services
sarahharvey · 17 weeks and 2 days ago
Thanks for the information keep sharing such informative post keep suggesting such post.
johnlead · 14 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: